Spaces:
Sleeping
Sleeping
Update text2generation.py
Browse files- text2generation.py +27 -25
text2generation.py
CHANGED
|
@@ -1,29 +1,31 @@
|
|
| 1 |
-
from transformers import pipeline, set_seed
|
| 2 |
import os
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
set_seed(42)
|
| 6 |
|
| 7 |
def generate_long_answer(question):
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
|
|
|
| 4 |
|
| 5 |
def generate_long_answer(question):
|
| 6 |
+
"""Generate detailed answers using OpenRouter's DeepSeek API"""
|
| 7 |
+
try:
|
| 8 |
+
response = requests.post(
|
| 9 |
+
url="https://openrouter.ai/api/v1/chat/completions",
|
| 10 |
+
headers={
|
| 11 |
+
"Authorization": f"Bearer {os.getenv('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')}",
|
| 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 |
+
timeout=30
|
| 26 |
+
)
|
| 27 |
+
response.raise_for_status()
|
| 28 |
+
return response.json()['choices'][0]['message']['content']
|
| 29 |
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"Error generating answer: {str(e)}"
|