sreepathi-ravikumar commited on
Commit
8865519
·
verified ·
1 Parent(s): 22eb489

Rename text2generation.py to audio_generator.py

Browse files
Files changed (2) hide show
  1. audio_generator.py +16 -0
  2. text2generation.py +0 -97
audio_generator.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from elevenlabs import generate, save
3
+
4
+ def generate_audio(text, filename="output_audio.mp3"):
5
+ api_key = os.getenv("ELEVENLABS_API_KEY")
6
+ if not api_key:
7
+ raise RuntimeError("Missing ElevenLabs API Key. Set ELEVENLABS_API_KEY in environment variables.")
8
+
9
+ audio = generate(
10
+ text=text,
11
+ voice="Rachel", # Change voice if needed
12
+ model="eleven_monolingual_v1",
13
+ api_key=api_key
14
+ )
15
+ save(audio, filename)
16
+ return filename
text2generation.py DELETED
@@ -1,97 +0,0 @@
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,
91
- "audio_script": audio_script,
92
- "summary": summary,
93
- "detailed_answer": detailed_answer
94
- }
95
-
96
- except Exception as e:
97
- return {"error": str(e)}