Spaces:
Sleeping
Sleeping
Upload summary_generator.py
Browse files- src/summary_generator.py +26 -0
src/summary_generator.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from src.api_clients import get_openrouter_models, openai_client
|
| 3 |
+
import os
|
| 4 |
+
def generate_summary_from_openrouter(article, prompt, model):
|
| 5 |
+
headers = {
|
| 6 |
+
"Authorization": f"Bearer {os.getenv('OPENROUTER_API_KEY')}",
|
| 7 |
+
"X-Title": "BrainDrive Summary Gen"
|
| 8 |
+
}
|
| 9 |
+
payload = {
|
| 10 |
+
"model": model,
|
| 11 |
+
"messages": [
|
| 12 |
+
{"role": "user", "content": f"{prompt}\n\n{article}"}
|
| 13 |
+
],
|
| 14 |
+
"max_tokens": 1000
|
| 15 |
+
}
|
| 16 |
+
res = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
|
| 17 |
+
return res.json()["choices"][0]["message"]["content"]
|
| 18 |
+
|
| 19 |
+
def is_prompt_valid_for_summary(prompt):
|
| 20 |
+
check_prompt = f"You are a summarization prompt checker. Determine if the following prompt is strictly asking for a summary: '{prompt}'. Reply 'Yes' or 'No'."
|
| 21 |
+
reply = openai_client.chat.completions.create(
|
| 22 |
+
model="gpt-4o-mini", # Note: Original had gpt-4.1-mini, but assuming typo; use gpt-4o-mini for compatibility
|
| 23 |
+
messages=[{"role": "user", "content": check_prompt}],
|
| 24 |
+
max_tokens=10
|
| 25 |
+
).choices[0].message.content.strip().lower()
|
| 26 |
+
return "yes" in reply
|