sreepathi-ravikumar commited on
Commit
e3d4280
·
verified ·
1 Parent(s): f2d29e5

Update text2generation.py

Browse files
Files changed (1) hide show
  1. text2generation.py +22 -27
text2generation.py CHANGED
@@ -1,30 +1,25 @@
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://sreepathi-ravikumar-sample.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.3,
23
- "max_tokens": 700
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']