alex-abb's picture
Update app.py
2e73823 verified
raw
history blame
2.2 kB
import os
import requests
api_token = os.environ.get("TOKEN")
API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-2-7b-chat-hf"
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_text):
output = query({
"inputs": f'''
system
You're going to deeply analyze the text I'm going to give you and you're only going to tell me which category it belongs 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
{pl7_text}
assistant
'''
})
print("API Response:", output) # Print the full API response
if isinstance(output, list) and len(output) > 0:
generated_text = output[0].get('generated_text', '')
print("Generated Text:", generated_text) # Print the generated text
# Extract the first non-empty line as the category
lines = [line.strip().lower() for line in generated_text.split('\n') if line.strip()]
if lines:
return lines[0]
return "unknown"
# Entrée personnalisée pour le test
custom_post_content = """
This is a sample post about fine-tuning models for specific tasks.
It discusses various techniques and best practices for adjusting models.
"""
print("Post content:")
print(custom_post_content)
print("\nAnalyzing sentiment...")
sentiment = analyze_sentiment(custom_post_content)
print(f"\nSentiment category: {sentiment}")