alex-abb commited on
Commit
f34d7d5
·
verified ·
1 Parent(s): 7e87179

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -4
app.py CHANGED
@@ -7,8 +7,13 @@ API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-2-7b-cha
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
  prompt = f'''<|begin_of_text|>
@@ -31,12 +36,20 @@ Respond only with the category name, without any additional explanation or text.
31
  <|start_header_id|>assistant<|end_header_id|>
32
  '''
33
 
 
34
  output = query({"inputs": prompt})
35
 
 
 
 
 
 
 
36
  if isinstance(output, list) and len(output) > 0:
37
  generated_text = output[0].get('generated_text', '')
38
  return generated_text.strip()
39
- return "Error: No response from the model"
 
40
 
41
  # Fetch a single post
42
  url = 'https://huggingface.co/posts'
@@ -47,7 +60,9 @@ if response.status_code == 200:
47
  pl7_element = soup.find(class_='pl-7')
48
  if pl7_element:
49
  pl7_text = pl7_element.text.strip()
50
- print("Analyzing post...")
 
 
51
  llm_response = analyze_sentiment(pl7_text)
52
  print(f"\nLLM Response:\n{llm_response}")
53
  else:
 
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|>
 
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
  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: