Spaces:
Sleeping
Sleeping
Update text2generation.py
Browse files- text2generation.py +20 -21
text2generation.py
CHANGED
|
@@ -1,29 +1,28 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
import json
|
| 4 |
|
| 5 |
def generate_long_answer(question):
|
| 6 |
"""Generate detailed answers using OpenRouter's DeepSeek API"""
|
| 7 |
try:
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
"
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
response.raise_for_status()
|
| 28 |
return response.json()['choices'][0]['message']['content']
|
| 29 |
|
|
|
|
| 1 |
import os
|
| 2 |
+
import httpx
|
|
|
|
| 3 |
|
| 4 |
def generate_long_answer(question):
|
| 5 |
"""Generate detailed answers using OpenRouter's DeepSeek API"""
|
| 6 |
try:
|
| 7 |
+
with httpx.Client(timeout=10.0) as client:
|
| 8 |
+
response = client.post(
|
| 9 |
+
url="https://openrouter.ai/api/v1/chat/completions",
|
| 10 |
+
headers={
|
| 11 |
+
"Authorization": f"Bearer {os.getenv('OPENROUTER_API_KEY')}",
|
| 12 |
+
"Content-Type": "application/json",
|
| 13 |
+
"HTTP-Referer": "https://your-huggingface-space-url.hf.space",
|
| 14 |
+
"X-Title": "Educational AI Assistant"
|
| 15 |
+
},
|
| 16 |
+
json={
|
| 17 |
+
"model": "deepseek/deepseek-chat-v3-0324:free",
|
| 18 |
+
"messages": [{
|
| 19 |
+
"role": "user",
|
| 20 |
+
"content": f"Provide a comprehensive educational answer with examples:\n\n{question}"
|
| 21 |
+
}],
|
| 22 |
+
"temperature": 0.7,
|
| 23 |
+
"max_tokens": 2000
|
| 24 |
+
}
|
| 25 |
+
)
|
| 26 |
response.raise_for_status()
|
| 27 |
return response.json()['choices'][0]['message']['content']
|
| 28 |
|