prompt revision 3
Browse files
components/generators/daily_feed.py
CHANGED
@@ -2,7 +2,7 @@ import os
|
|
2 |
import sys
|
3 |
import json
|
4 |
import requests
|
5 |
-
from
|
6 |
|
7 |
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
8 |
|
@@ -51,38 +51,50 @@ HEADERS = {
|
|
51 |
"Content-Type": "application/json"
|
52 |
}
|
53 |
|
54 |
-
def call_mistral(prompt: str
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
59 |
payload = {
|
60 |
-
"inputs":
|
61 |
"parameters": {
|
62 |
-
"max_new_tokens":
|
63 |
-
"temperature":
|
64 |
}
|
65 |
}
|
66 |
|
|
|
|
|
|
|
|
|
|
|
67 |
try:
|
68 |
-
response = requests.post(MISTRAL_URL, headers=
|
69 |
response.raise_for_status()
|
70 |
data = response.json()
|
71 |
|
72 |
-
# Handle both
|
73 |
-
if isinstance(data, list) and data:
|
74 |
-
return data[0]
|
75 |
-
|
76 |
return data["generated_text"].strip()
|
|
|
|
|
|
|
77 |
|
78 |
-
except RequestException as e:
|
79 |
-
print("❌
|
80 |
if e.response is not None:
|
81 |
-
print("
|
82 |
except Exception as e:
|
83 |
-
print("❌
|
|
|
|
|
84 |
|
85 |
-
return ""
|
86 |
|
87 |
|
88 |
# ✂️ Generate summaries per topic
|
|
|
2 |
import sys
|
3 |
import json
|
4 |
import requests
|
5 |
+
from typing import Optional
|
6 |
|
7 |
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
8 |
|
|
|
51 |
"Content-Type": "application/json"
|
52 |
}
|
53 |
|
54 |
+
def call_mistral(prompt: str) -> Optional[str]:
|
55 |
+
if not prompt or len(prompt.strip()) < 10:
|
56 |
+
print(f"⚠️ Skipping empty or invalid prompt:\n{prompt}\n")
|
57 |
+
return None
|
58 |
+
|
59 |
+
# Wrap the prompt properly
|
60 |
+
formatted_prompt = f"[INST] {prompt.strip()} [/INST]"
|
61 |
+
|
62 |
payload = {
|
63 |
+
"inputs": formatted_prompt,
|
64 |
"parameters": {
|
65 |
+
"max_new_tokens": 120,
|
66 |
+
"temperature": 0.7,
|
67 |
}
|
68 |
}
|
69 |
|
70 |
+
headers = {
|
71 |
+
"Authorization": f"Bearer {HF_TOKEN}",
|
72 |
+
"Content-Type": "application/json"
|
73 |
+
}
|
74 |
+
|
75 |
try:
|
76 |
+
response = requests.post(MISTRAL_URL, headers=headers, json=payload, timeout=30)
|
77 |
response.raise_for_status()
|
78 |
data = response.json()
|
79 |
|
80 |
+
# Handle both dict and list responses
|
81 |
+
if isinstance(data, list) and "generated_text" in data[0]:
|
82 |
+
return data[0]["generated_text"].strip()
|
83 |
+
elif isinstance(data, dict) and "generated_text" in data:
|
84 |
return data["generated_text"].strip()
|
85 |
+
else:
|
86 |
+
print("⚠️ Unexpected response format:", data)
|
87 |
+
return None
|
88 |
|
89 |
+
except requests.RequestException as e:
|
90 |
+
print("❌ HF Endpoint error:", str(e))
|
91 |
if e.response is not None:
|
92 |
+
print("❌ Response body:", e.response.text[:300])
|
93 |
except Exception as e:
|
94 |
+
print("❌ Unknown error:", str(e))
|
95 |
+
|
96 |
+
return None
|
97 |
|
|
|
98 |
|
99 |
|
100 |
# ✂️ Generate summaries per topic
|