alex-abb commited on
Commit
6eed6c8
·
verified ·
1 Parent(s): b6b2412

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -34
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/Meta-Llama-3-8B-Instruct"
7
  headers = {"Authorization": f"Bearer {api_token}"}
8
 
9
  def query(payload):
10
- try:
11
- response = requests.post(API_URL, headers=headers, json=payload)
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
- prompt = f'''<|begin_of_text|>
20
- <|start_header_id|>system<|end_header_id|>
 
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
- <|eot_id|>
33
- <|start_header_id|>user<|end_header_id|>
34
  {pl7_text}
35
- <|eot_id|>
36
- <|start_header_id|>assistant<|end_header_id|>
37
  '''
 
38
 
39
- print("Sending request to API...")
40
- output = query({"inputs": prompt})
41
-
42
- if output is None:
43
- return "Error: Failed to get response from API"
44
-
45
- print("Raw API response:")
46
- print(output)
47
-
48
- if isinstance(output, list) and len(output) > 0:
49
- generated_text = output[0].get('generated_text', '')
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 (first 100 characters):")
64
- print(pl7_text[:100] + "..." if len(pl7_text) > 100 else pl7_text)
65
- print("\nAnalyzing post...")
66
- llm_response = analyze_sentiment(pl7_text)
67
- print(f"\nLLM Response:\n{llm_response}")
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}")