alex-abb's picture
Update app.py
5808ced verified
import requests
import json
import os
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(text):
output = query({
"inputs": f'''
system
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.
user
{text}
assistant
'''
})
if isinstance(output, list) and len(output) > 0:
response = output[0].get('generated_text', '').strip().lower()
questions = response.count('questions')
chat_model_llm = response.count('chatmodel/llm')
other = response.count('other')
image_generation = response.count("image_generation")
fine_tuning = response.count("fine_tuning")
ethics_bias = response.count("ethics_bias")
datasets = response.count("datasets")
tools_libraries = response.count("tools_libraries")
tutorials_guides = response.count("tutorials_guides")
debugging = response.count("debugging")
if questions == 2:
return 'questions'
elif chat_model_llm == 2:
return 'Chat Model/LLM'
elif other == 2:
return "Other"
elif image_generation == 2:
return "Image Generation"
elif fine_tuning == 2:
return "Fine-tuning"
elif ethics_bias == 2:
return "Ethics and Bias"
elif datasets == 2:
return "Datasets"
elif tools_libraries == 2:
return "Tools and Libraries"
elif tutorials_guides == 2:
return "Tutorials and Guides"
elif debugging == 2:
return "Debugging"
else:
return f"Erreur: Réponse ambiguë - '{response}'"
# URL de base de l'API
base_url = "https://huggingface.co/api/posts"
# Paramètres pour la pagination
skip = 0 # Nombre d'éléments à sauter
limit = 100 # Nombre maximal d'éléments à récupérer par requête
# Liste pour stocker tous les posts avec leur texte
all_posts_with_text = []
while True:
# Construire l'URL avec les paramètres de pagination
url = f"{base_url}?skip={skip}&limit={limit}&sort=recent"
# Effectuer une requête HTTP pour récupérer les données
response = requests.get(url)
# Vérifier si la requête a réussi
if response.status_code == 200:
# Charger les données JSON à partir du contenu de la réponse
data = response.json()
# Vérifier s'il y a des posts à ajouter
if not data["socialPosts"]:
break # Sortir de la boucle si aucun post n'est retourné
# Ajouter les posts récupérés à la liste avec leur texte
for post in data["socialPosts"]:
post_text = ""
for item in post["content"]:
if item["type"] == "text":
post_text += item["value"] + " "
all_posts_with_text.append({"slug": post["slug"], "text": post_text.strip()})
# Mettre à jour le paramètre skip pour la prochaine requête
skip += limit
else:
print(f"Erreur lors de la récupération des données: {response.status_code}")
break
# Maintenant, all_posts_with_text contient tous les posts récupérés avec leur texte
questions_count = 0
chat_model_llm_count = 0
other_count = 0
image_generation_count = 0
fine_tuning_count = 0
ethics_and_bias_count = 0
datasets_count = 0
tools_and_libraries_count = 0
tutorials_and_guides_count = 0
debugging_count = 0
# Appliquer votre algorithme d'analyse à tous les posts
for i, post in enumerate(all_posts_with_text, 1):
slug = post["slug"]
text = post["text"]
# Appeler votre algorithme d'analyse
resultat = analyze_sentiment(text)
if resultat == 'questions':
questions_count += 1
elif resultat == "Chat Model/LLM":
chat_model_llm_count += 1
elif resultat == 'Other':
other_count += 1
elif resultat == "Image Generation":
image_generation_count += 1
elif resultat == "Fine-tuning":
fine_tuning_count += 1
elif resultat == "Ethics and Bias":
ethics_and_bias_count += 1
elif resultat == 'Datasets':
datasets_count += 1
elif resultat == 'Tools and Libraries':
tools_and_libraries_count += 1
elif resultat == "Tutorials and Guides":
tutorials_and_guides_count += 1
elif resultat == "Debugging":
debugging_count += 1
# Vous pouvez adapter votre algorithme d'analyse selon vos besoins spécifiques
print(f"Questions: {questions_count}")
print(f"Chat Model/LLM: {chat_model_llm_count}")
print(f"Other: {other_count}")
print(f"Image Generation: {image_generation_count}")
print(f"Fine-tuning: {fine_tuning_count}")
print(f"Ethics and Bias: {ethics_and_bias_count}")
print(f"Datasets: {datasets_count}")
print(f"Tools and Libraries: {tools_and_libraries_count}")
print(f"Tutorials and Guides: {tutorials_and_guides_count}")
print(f"Debugging: {debugging_count}")