Spaces:
Sleeping
Sleeping
Update text2generation.py
Browse files- text2generation.py +22 -27
text2generation.py
CHANGED
|
@@ -1,30 +1,25 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import httpx
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
}
|
| 25 |
-
)
|
| 26 |
-
response.raise_for_status()
|
| 27 |
-
return response.json()['choices'][0]['message']['content']
|
| 28 |
-
|
| 29 |
-
except Exception as e:
|
| 30 |
-
return f"Error generating answer: {str(e)}"
|
|
|
|
| 1 |
import os
|
| 2 |
+
import asyncio
|
| 3 |
import httpx
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
+
load_dotenv()
|
| 7 |
+
|
| 8 |
+
async def generate_fast_answer(question):
|
| 9 |
+
async with httpx.AsyncClient(timeout=10.0) as client:
|
| 10 |
+
response = await client.post(
|
| 11 |
+
"https://openrouter.ai/api/v1/chat/completions",
|
| 12 |
+
headers={
|
| 13 |
+
"Authorization": f"Bearer {os.getenv('OPENROUTER_API_KEY')}",
|
| 14 |
+
"Content-Type": "application/json",
|
| 15 |
+
"HTTP-Referer": "https://sreepathi-ravikumar-sample.hf.space",
|
| 16 |
+
"X-Title": "Educational AI Assistant"
|
| 17 |
+
},
|
| 18 |
+
json={
|
| 19 |
+
"model": "deepseek/deepseek-chat-v3-0324:free",
|
| 20 |
+
"messages": [{"role": "user", "content": question}],
|
| 21 |
+
"temperature": 0.3,
|
| 22 |
+
"max_tokens": 700
|
| 23 |
+
}
|
| 24 |
+
)
|
| 25 |
+
return response.json()['choices'][0]['message']['content']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|