Create text2generation.py
Browse files- text2generation.py +76 -0
text2generation.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from openai import OpenAI
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
client = OpenAI(
|
| 5 |
+
base_url="https://openrouter.ai/api/v1",
|
| 6 |
+
api_key=os.getenv("OPENROUTER_API_KEY")
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
def generate_educational_content(question):
|
| 10 |
+
"""Generate all content formats for a question"""
|
| 11 |
+
try:
|
| 12 |
+
# Step 1: Get detailed explanation
|
| 13 |
+
detailed_response = client.chat.completions.create(
|
| 14 |
+
model="deepseek/deepseek-chat-v3-0324:free",
|
| 15 |
+
messages=[{
|
| 16 |
+
"role": "user",
|
| 17 |
+
"content": f"""As an expert educator, provide:
|
| 18 |
+
1. Key Concepts 2. Examples 3. Common Mistakes
|
| 19 |
+
Question: {question}"""
|
| 20 |
+
}],
|
| 21 |
+
temperature=0.7,
|
| 22 |
+
max_tokens=2000
|
| 23 |
+
)
|
| 24 |
+
detailed_answer = detailed_response.choices[0].message.content
|
| 25 |
+
|
| 26 |
+
# Step 2: Generate video script
|
| 27 |
+
video_script = client.chat.completions.create(
|
| 28 |
+
model="deepseek/deepseek-chat-v3-0324:free",
|
| 29 |
+
messages=[{
|
| 30 |
+
"role": "user",
|
| 31 |
+
"content": f"""Convert this into a video script:
|
| 32 |
+
{detailed_answer}
|
| 33 |
+
Format for each scene:
|
| 34 |
+
[Duration]: 5-8 seconds
|
| 35 |
+
[Visual]: Detailed description + Pexels keywords
|
| 36 |
+
[Voiceover]: Exact narration text
|
| 37 |
+
[Transition]: To next scene"""
|
| 38 |
+
}],
|
| 39 |
+
temperature=0.5,
|
| 40 |
+
max_tokens=1500
|
| 41 |
+
).choices[0].message.content
|
| 42 |
+
|
| 43 |
+
# Step 3: Generate audio script
|
| 44 |
+
audio_script = client.chat.completions.create(
|
| 45 |
+
model="deepseek/deepseek-chat-v3-0324:free",
|
| 46 |
+
messages=[{
|
| 47 |
+
"role": "user",
|
| 48 |
+
"content": f"""Convert this into engaging narration:
|
| 49 |
+
{detailed_answer}
|
| 50 |
+
Style: Friendly tutor (pause between concepts)"""
|
| 51 |
+
}],
|
| 52 |
+
temperature=0.8,
|
| 53 |
+
max_tokens=1000
|
| 54 |
+
).choices[0].message.content
|
| 55 |
+
|
| 56 |
+
# Step 4: Generate summary
|
| 57 |
+
summary = client.chat.completions.create(
|
| 58 |
+
model="deepseek/deepseek-chat-v3-0324:free",
|
| 59 |
+
messages=[{
|
| 60 |
+
"role": "user",
|
| 61 |
+
"content": f"""Create a 3-sentence student summary:
|
| 62 |
+
{detailed_answer}"""
|
| 63 |
+
}],
|
| 64 |
+
temperature=0.3,
|
| 65 |
+
max_tokens=300
|
| 66 |
+
).choices[0].message.content
|
| 67 |
+
|
| 68 |
+
return {
|
| 69 |
+
"video_script": video_script,
|
| 70 |
+
"audio_script": audio_script,
|
| 71 |
+
"summary": summary,
|
| 72 |
+
"detailed_answer": detailed_answer
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
except Exception as e:
|
| 76 |
+
return {"error": str(e)}
|