Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,21 +3,17 @@ import requests
|
|
3 |
from bs4 import BeautifulSoup
|
4 |
|
5 |
api_token = os.environ.get("TOKEN")
|
6 |
-
API_URL = "https://api-inference.huggingface.co/models/meta-llama/
|
7 |
headers = {"Authorization": f"Bearer {api_token}"}
|
8 |
|
9 |
def query(payload):
|
10 |
-
|
11 |
-
|
12 |
-
response.raise_for_status() # Raise an exception for bad status codes
|
13 |
-
return response.json()
|
14 |
-
except requests.exceptions.RequestException as e:
|
15 |
-
print(f"Error making request to API: {e}")
|
16 |
-
return None
|
17 |
|
18 |
def analyze_sentiment(pl7_text):
|
19 |
-
|
20 |
-
|
|
|
21 |
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:
|
22 |
For posts that talk about chat models/LLM, return "Chatmodel/LLM"
|
23 |
For posts that talk about image generation models, return "image_generation"
|
@@ -29,27 +25,25 @@ For posts about tools and libraries, return "tools_libraries"
|
|
29 |
For posts containing tutorials and guides, return "tutorials_guides"
|
30 |
For posts about debugging and problem-solving, return "debugging"
|
31 |
Respond only with the category name, without any additional explanation or text.
|
32 |
-
|
33 |
-
|
34 |
{pl7_text}
|
35 |
-
|
36 |
-
|
37 |
'''
|
|
|
38 |
|
39 |
-
print("
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
return generated_text.strip()
|
51 |
-
else:
|
52 |
-
return "Error: Unexpected response format from API"
|
53 |
|
54 |
# Fetch a single post
|
55 |
url = 'https://huggingface.co/posts'
|
@@ -60,12 +54,12 @@ if response.status_code == 200:
|
|
60 |
pl7_element = soup.find(class_='pl-7')
|
61 |
if pl7_element:
|
62 |
pl7_text = pl7_element.text.strip()
|
63 |
-
print("Post content
|
64 |
-
print(pl7_text
|
65 |
-
print("\nAnalyzing
|
66 |
-
|
67 |
-
print(f"\
|
68 |
else:
|
69 |
print("No post found with class 'pl-7'")
|
70 |
else:
|
71 |
-
print(f"Error {response.status_code} when retrieving {url}")
|
|
|
3 |
from bs4 import BeautifulSoup
|
4 |
|
5 |
api_token = os.environ.get("TOKEN")
|
6 |
+
API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-2-7b-chat-hf"
|
7 |
headers = {"Authorization": f"Bearer {api_token}"}
|
8 |
|
9 |
def query(payload):
|
10 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
11 |
+
return response.json()
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
def analyze_sentiment(pl7_text):
|
14 |
+
output = query({
|
15 |
+
"inputs": f'''
|
16 |
+
system
|
17 |
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:
|
18 |
For posts that talk about chat models/LLM, return "Chatmodel/LLM"
|
19 |
For posts that talk about image generation models, return "image_generation"
|
|
|
25 |
For posts containing tutorials and guides, return "tutorials_guides"
|
26 |
For posts about debugging and problem-solving, return "debugging"
|
27 |
Respond only with the category name, without any additional explanation or text.
|
28 |
+
|
29 |
+
user
|
30 |
{pl7_text}
|
31 |
+
|
32 |
+
assistant
|
33 |
'''
|
34 |
+
})
|
35 |
|
36 |
+
print("API Response:", output) # Print the full API response
|
37 |
+
|
38 |
+
# Extract the generated text
|
39 |
+
generated_text = output.get('generated_text', '')
|
40 |
+
print("Generated Text:", generated_text) # Print the generated text
|
41 |
+
|
42 |
+
# Extract the first non-empty line as the category
|
43 |
+
lines = [line.strip().lower() for line in generated_text.split('\n') if line.strip()]
|
44 |
+
if lines:
|
45 |
+
return lines[0]
|
46 |
+
return "unknown"
|
|
|
|
|
|
|
47 |
|
48 |
# Fetch a single post
|
49 |
url = 'https://huggingface.co/posts'
|
|
|
54 |
pl7_element = soup.find(class_='pl-7')
|
55 |
if pl7_element:
|
56 |
pl7_text = pl7_element.text.strip()
|
57 |
+
print("Post content:")
|
58 |
+
print(pl7_text)
|
59 |
+
print("\nAnalyzing sentiment...")
|
60 |
+
sentiment = analyze_sentiment(pl7_text)
|
61 |
+
print(f"\nSentiment category: {sentiment}")
|
62 |
else:
|
63 |
print("No post found with class 'pl-7'")
|
64 |
else:
|
65 |
+
print(f"Error {response.status_code} when retrieving {url}")
|