Spaces:
Sleeping
Sleeping
Update text2generation.py
Browse files- text2generation.py +77 -60
text2generation.py
CHANGED
|
@@ -1,73 +1,90 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
# Setup your OpenRouter API key and base
|
| 5 |
-
openai.api_key = os.getenv("OPENROUTER_API_KEY")
|
| 6 |
-
openai.api_base = "https://openrouter.ai/api/v1"
|
| 7 |
-
|
| 8 |
-
# Optional but recommended: Set headers via environment if needed
|
| 9 |
-
# openai._default_headers.update({
|
| 10 |
-
# "HTTP-Referer": "https://sreepathi-ravikumar-backendprocess.hf.space",
|
| 11 |
-
# "X-Title": "Educational AI Assistant"
|
| 12 |
-
# })
|
| 13 |
|
| 14 |
def generate_educational_content(question):
|
| 15 |
try:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Step 2: Video script
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
# Step 3: Audio script
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
# Step 4: Summary
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
return {
|
| 73 |
"video_script": video_script,
|
|
|
|
| 1 |
import os
|
| 2 |
+
import httpx
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def generate_educational_content(question):
|
| 5 |
try:
|
| 6 |
+
headers = {
|
| 7 |
+
"Authorization": f"Bearer {os.getenv('OPENROUTER_API_KEY')}",
|
| 8 |
+
"Content-Type": "application/json",
|
| 9 |
+
"HTTP-Referer": "https://sreepathi-ravikumar-backendprocess.hf.space",
|
| 10 |
+
"X-Title": "Educational AI Assistant"
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
# Step 1: Detailed explanation
|
| 14 |
+
with httpx.Client(timeout=15.0) as client:
|
| 15 |
+
detailed_response = client.post(
|
| 16 |
+
url="https://openrouter.ai/api/v1/chat/completions",
|
| 17 |
+
headers=headers,
|
| 18 |
+
json={
|
| 19 |
+
"model": "deepseek/deepseek-chat-v3-0324:free",
|
| 20 |
+
"messages": [{
|
| 21 |
+
"role": "user",
|
| 22 |
+
"content": f"""As an expert educator, provide:
|
| 23 |
+
1. Key Concepts
|
| 24 |
+
2. Examples
|
| 25 |
+
3. Common Mistakes
|
| 26 |
+
Question: {question}"""
|
| 27 |
+
}],
|
| 28 |
+
"temperature": 0.7,
|
| 29 |
+
"max_tokens": 2000
|
| 30 |
+
}
|
| 31 |
+
)
|
| 32 |
+
detailed_answer = detailed_response.json()['choices'][0]['message']['content']
|
| 33 |
|
| 34 |
# Step 2: Video script
|
| 35 |
+
with httpx.Client(timeout=15.0) as client:
|
| 36 |
+
video_response = client.post(
|
| 37 |
+
url="https://openrouter.ai/api/v1/chat/completions",
|
| 38 |
+
headers=headers,
|
| 39 |
+
json={
|
| 40 |
+
"model": "deepseek/deepseek-chat-v3-0324:free",
|
| 41 |
+
"messages": [{
|
| 42 |
+
"role": "user",
|
| 43 |
+
"content": f"""Convert this into a video script:\n\n{detailed_answer}\n\nFormat:
|
| 44 |
+
[Duration]: 5–8 sec
|
| 45 |
+
[Visual]: Description + Pexels keywords
|
| 46 |
+
[Voiceover]: Narration
|
| 47 |
+
[Transition]: Next scene"""
|
| 48 |
+
}],
|
| 49 |
+
"temperature": 0.5,
|
| 50 |
+
"max_tokens": 1500
|
| 51 |
+
}
|
| 52 |
+
)
|
| 53 |
+
video_script = video_response.json()['choices'][0]['message']['content']
|
| 54 |
|
| 55 |
# Step 3: Audio script
|
| 56 |
+
with httpx.Client(timeout=15.0) as client:
|
| 57 |
+
audio_response = client.post(
|
| 58 |
+
url="https://openrouter.ai/api/v1/chat/completions",
|
| 59 |
+
headers=headers,
|
| 60 |
+
json={
|
| 61 |
+
"model": "deepseek/deepseek-chat-v3-0324:free",
|
| 62 |
+
"messages": [{
|
| 63 |
+
"role": "user",
|
| 64 |
+
"content": f"""Convert this into an engaging narration with a friendly tone:\n\n{detailed_answer}"""
|
| 65 |
+
}],
|
| 66 |
+
"temperature": 0.8,
|
| 67 |
+
"max_tokens": 1000
|
| 68 |
+
}
|
| 69 |
+
)
|
| 70 |
+
audio_script = audio_response.json()['choices'][0]['message']['content']
|
| 71 |
|
| 72 |
# Step 4: Summary
|
| 73 |
+
with httpx.Client(timeout=15.0) as client:
|
| 74 |
+
summary_response = client.post(
|
| 75 |
+
url="https://openrouter.ai/api/v1/chat/completions",
|
| 76 |
+
headers=headers,
|
| 77 |
+
json={
|
| 78 |
+
"model": "deepseek/deepseek-chat-v3-0324:free",
|
| 79 |
+
"messages": [{
|
| 80 |
+
"role": "user",
|
| 81 |
+
"content": f"""Create a simple 3-sentence student summary:\n\n{detailed_answer}"""
|
| 82 |
+
}],
|
| 83 |
+
"temperature": 0.3,
|
| 84 |
+
"max_tokens": 300
|
| 85 |
+
}
|
| 86 |
+
)
|
| 87 |
+
summary = summary_response.json()['choices'][0]['message']['content']
|
| 88 |
|
| 89 |
return {
|
| 90 |
"video_script": video_script,
|