import os import requests import gradio as gr from bs4 import BeautifulSoup api_token = os.environ.get("TOKEN") API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct" headers = {"Authorization": f"Bearer {api_token}"} def query(payload): response = requests.post(API_URL, headers=headers, json=payload) return response.json() def analyze_sentiment(pl7_texts): output = query({ "inputs": f'''<|begin_of_text|> <|start_header_id|>system<|end_header_id|> You're going to deeply analyze the texts I'm going to give you and you're only going to tell me which category they belong to by answering only the words that correspond to the following categories: For posts that talk about chat models/LLM, return "Chatmodel/LLM" For posts that talk about image generation models, return "image_generation" For texts that ask for information from the community, return "questions" For posts about fine-tuning or model adjustment, return "fine_tuning" For posts related to ethics and bias in AI, return "ethics_bias" For posts about datasets and data preparation, return "datasets" For posts about tools and libraries, return "tools_libraries" For posts containing tutorials and guides, return "tutorials_guides" For posts about debugging and problem-solving, return "debugging" Respond only with the category name, without any additional explanation or text. <|eot_id|> <|start_header_id|>user<|end_header_id|> {pl7_texts} <|eot_id|> <|start_header_id|>assistant<|end_header_id|> ''' }) if isinstance(output, list) and len(output) > 0: response = output[0].get('generated_text', '').strip().lower() categories = { 'questions': 'Questions', 'chatmodel/llm': 'Chat Model/LLM', 'image_generation': 'Image Generation', 'fine_tuning': 'Fine-tuning', 'ethics_bias': 'Ethics and Bias', 'datasets': 'Datasets', 'tools_libraries': 'Tools and Libraries', 'tutorials_guides': 'Tutorials and Guides', 'debugging': 'Debugging' } return categories.get(response, f"Error: Ambiguous response - '{response}'") return "Error: No valid response received" url = 'https://huggingface.co/posts' response = requests.get(url) if response.status_code == 200: soup = BeautifulSoup(response.content, 'html.parser') pl7_elements = soup.find_all(class_='pl-7') pl7_texts = [element.text.strip() for element in pl7_elements] for idx, text in enumerate(pl7_texts, start=1): print(f"Text pl-7 {idx}: {text}") sentiment = analyze_sentiment(text) print(f"Sentiment: {sentiment}\n") if len(pl7_texts) >= 4: print(f"Content of pl7_text_1: {pl7_texts[0]}") print(f"Content of pl7_text_2: {pl7_texts[1]}") else: print("Not enough pl-7 elements found") else: print(f"Error {response.status_code} when retrieving {url}")