sreepathi-ravikumar commited on
Commit
b038aaa
·
verified ·
1 Parent(s): 3eb4e3e

Update text2generation.py

Browse files
Files changed (1) hide show
  1. text2generation.py +77 -60
text2generation.py CHANGED
@@ -1,73 +1,90 @@
1
  import os
2
- import openai
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
- # Step 1: Get detailed explanation
17
- detailed_response = openai.ChatCompletion.create(
18
- model="deepseek/deepseek-chat-v3-0324:free",
19
- messages=[{
20
- "role": "user",
21
- "content": f"""As an expert educator, provide:
22
- 1. Key Concepts 2. Examples 3. Common Mistakes
23
- Question: {question}"""
24
- }],
25
- temperature=0.7,
26
- max_tokens=2000
27
- )
28
- detailed_answer = detailed_response.choices[0].message.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
  # Step 2: Video script
31
- video_script = openai.ChatCompletion.create(
32
- model="deepseek/deepseek-chat-v3-0324:free",
33
- messages=[{
34
- "role": "user",
35
- "content": f"""Convert this into a video script:
36
- {detailed_answer}
37
- Format for each scene:
38
- [Duration]: 5-8 seconds
39
- [Visual]: Detailed description + Pexels keywords
40
- [Voiceover]: Exact narration text
41
- [Transition]: To next scene"""
42
- }],
43
- temperature=0.5,
44
- max_tokens=1500
45
- ).choices[0].message.content
 
 
 
 
46
 
47
  # Step 3: Audio script
48
- audio_script = openai.ChatCompletion.create(
49
- model="deepseek/deepseek-chat-v3-0324:free",
50
- messages=[{
51
- "role": "user",
52
- "content": f"""Convert this into engaging narration:
53
- {detailed_answer}
54
- Style: Friendly tutor (pause between concepts)"""
55
- }],
56
- temperature=0.8,
57
- max_tokens=1000
58
- ).choices[0].message.content
 
 
 
 
59
 
60
  # Step 4: Summary
61
- summary = openai.ChatCompletion.create(
62
- model="deepseek/deepseek-chat-v3-0324:free",
63
- messages=[{
64
- "role": "user",
65
- "content": f"""Create a 3-sentence student summary:
66
- {detailed_answer}"""
67
- }],
68
- temperature=0.3,
69
- max_tokens=300
70
- ).choices[0].message.content
 
 
 
 
 
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,