ragV98 commited on
Commit
27120a6
Β·
1 Parent(s): 5c17060

func clean up

Browse files
Files changed (1) hide show
  1. components/generators/daily_feed.py +23 -19
components/generators/daily_feed.py CHANGED
@@ -15,7 +15,7 @@ Settings.llm = None
15
  # πŸ” Environment variables
16
  REDIS_URL = os.environ.get("UPSTASH_REDIS_URL", "redis://localhost:6379")
17
  REDIS_KEY = os.environ.get("UPSTASH_REDIS_TOKEN")
18
- HF_ENDPOINT_URL = os.environ.get("MISTRAL_URL")
19
  HF_TOKEN = os.environ.get("HF_TOKEN")
20
 
21
  # βœ… Redis client
@@ -46,32 +46,36 @@ def build_prompt(content: str, topic: str) -> str:
46
  return f"<s>[INST]{base_instruction}\n\n{tail}[/INST]</s>"
47
 
48
  # πŸ” Call Mistral using HF Inference Endpoint
49
- def call_mistral(prompt: str) -> str:
 
 
 
 
50
  payload = {
51
- "inputs": prompt,
52
- "parameters": {
53
- "max_new_tokens": 128,
54
- "temperature": 0.7,
55
- },
56
  }
 
57
  try:
58
- response = requests.post(HF_ENDPOINT_URL, headers=HEADERS, json=payload, timeout=90)
59
  response.raise_for_status()
60
  data = response.json()
61
 
 
62
  if isinstance(data, list) and data:
63
- return data[0].get("generated_text", "").strip()
64
- if isinstance(data, dict) and "generated_text" in data:
65
- return data["generated_text"].strip()
66
-
67
- except requests.exceptions.RequestException as e:
68
- print("❌ HF Endpoint error:", str(e))
69
- if e.response is not None:
70
- print("Endpoint said:", e.response.text[:300])
71
- except Exception as e:
72
- print("❌ Unknown error:", str(e))
73
 
74
- return ""
 
 
 
 
 
 
 
75
 
76
  # βœ‚οΈ Summarize top N documents
77
  def summarize_topic(docs: List[str], topic: str) -> List[Dict]:
 
15
  # πŸ” Environment variables
16
  REDIS_URL = os.environ.get("UPSTASH_REDIS_URL", "redis://localhost:6379")
17
  REDIS_KEY = os.environ.get("UPSTASH_REDIS_TOKEN")
18
+ MISTRAL_URL = os.environ.get("MISTRAL_URL")
19
  HF_TOKEN = os.environ.get("HF_TOKEN")
20
 
21
  # βœ… Redis client
 
46
  return f"<s>[INST]{base_instruction}\n\n{tail}[/INST]</s>"
47
 
48
  # πŸ” Call Mistral using HF Inference Endpoint
49
+ def call_mistral(prompt: str) -> Optional[str]:
50
+ headers = {
51
+ "Authorization": f"Bearer {HF_TOKEN}",
52
+ "Content-Type": "application/json"
53
+ }
54
  payload = {
55
+ "inputs": prompt
 
 
 
 
56
  }
57
+
58
  try:
59
+ response = requests.post(MISTRAL_URL, headers=headers, json=payload, timeout=20)
60
  response.raise_for_status()
61
  data = response.json()
62
 
63
+ # Get the generated text
64
  if isinstance(data, list) and data:
65
+ raw_output = data[0].get("generated_text", "")
66
+ elif isinstance(data, dict):
67
+ raw_output = data.get("generated_text", "")
68
+ else:
69
+ return None
 
 
 
 
 
70
 
71
+ # βœ… Extract only the portion after the [/INST]</s> marker
72
+ if "[/INST]</s>" in raw_output:
73
+ return raw_output.split("[/INST]</s>")[-1].strip()
74
+ return raw_output.strip()
75
+
76
+ except Exception as e:
77
+ print(f"⚠️ Mistral error: {e}")
78
+ return None
79
 
80
  # βœ‚οΈ Summarize top N documents
81
  def summarize_topic(docs: List[str], topic: str) -> List[Dict]: