Update app.py
Browse files
app.py
CHANGED
@@ -2,21 +2,38 @@ import gradio as gr
|
|
2 |
from groq import Groq
|
3 |
import os
|
4 |
import threading
|
5 |
-
from moviepy.editor import TextClip, concatenate_videoclips, AudioFileClip, ColorClip
|
6 |
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
#
|
9 |
-
|
|
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
# Stop event for threading (image generation)
|
16 |
stop_event = threading.Event()
|
17 |
|
18 |
# Function to generate tutor output (lesson, question, feedback)
|
19 |
def generate_tutor_output(subject, difficulty, student_input):
|
|
|
|
|
|
|
20 |
prompt = f"""
|
21 |
You are an expert tutor in {subject} at the {difficulty} level.
|
22 |
The student has provided the following input: "{student_input}"
|
@@ -29,23 +46,28 @@ def generate_tutor_output(subject, difficulty, student_input):
|
|
29 |
Format your response as a JSON object with keys: "lesson", "question", "feedback"
|
30 |
"""
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
45 |
|
46 |
# Function to generate images based on model selection
|
47 |
def generate_images(text, selected_model):
|
48 |
stop_event.clear()
|
|
|
|
|
49 |
|
50 |
if selected_model == "Model 1 (Turbo Realism)":
|
51 |
model = model1
|
@@ -54,19 +76,27 @@ def generate_images(text, selected_model):
|
|
54 |
else:
|
55 |
return ["Invalid model selection."] * 3
|
56 |
|
|
|
|
|
|
|
57 |
results = []
|
58 |
for i in range(3):
|
59 |
if stop_event.is_set():
|
60 |
return ["Image generation stopped by user."] * 3
|
61 |
-
|
62 |
modified_text = f"{text} variation {i+1}"
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
66 |
return results
|
67 |
|
68 |
-
#
|
69 |
def generate_text_to_video(text):
|
|
|
|
|
|
|
70 |
try:
|
71 |
# Generate narration using Groq (text-to-speech simulation)
|
72 |
narration_prompt = f"Convert this text to a natural-sounding narration: {text}"
|
@@ -83,11 +113,9 @@ def generate_text_to_video(text):
|
|
83 |
)
|
84 |
narration_text = narration_response.choices[0].message.content
|
85 |
|
86 |
-
# Simulate TTS
|
87 |
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_audio:
|
88 |
-
|
89 |
-
# For now, we'll simulate with a silent audio clip
|
90 |
-
audio_duration = len(narration_text.split()) / 2 # Rough estimate: 2 words per second
|
91 |
audio = ColorClip(size=(100, 100), color=(0, 0, 0), duration=audio_duration).set_audio(None)
|
92 |
audio.write_audiofile(temp_audio.name, fps=44100, logger=None)
|
93 |
|
@@ -98,7 +126,7 @@ def generate_text_to_video(text):
|
|
98 |
for i in range(0, len(words), chunk_size):
|
99 |
chunk = " ".join(words[i:i + chunk_size])
|
100 |
clip = TextClip(chunk, fontsize=50, color='white', size=(1280, 720), bg_color='black')
|
101 |
-
clip = clip.set_duration(audio_duration / (len(words) / chunk_size))
|
102 |
clips.append(clip)
|
103 |
|
104 |
# Concatenate clips into a single video
|
@@ -115,32 +143,31 @@ def generate_text_to_video(text):
|
|
115 |
|
116 |
# Clean up temporary audio file
|
117 |
os.unlink(temp_audio.name)
|
118 |
-
|
119 |
return video_path
|
120 |
except Exception as e:
|
|
|
121 |
return f"Error generating video: {str(e)}"
|
122 |
|
123 |
-
#
|
124 |
-
with gr.Blocks() as demo:
|
125 |
-
gr.Markdown("# 🎓 Your AI Tutor with Visuals &
|
126 |
|
127 |
-
#
|
128 |
with gr.Row():
|
129 |
with gr.Column(scale=2):
|
130 |
subject = gr.Dropdown(
|
131 |
["Math", "Science", "History", "Literature", "Code", "AI"],
|
132 |
label="Subject",
|
133 |
-
|
134 |
)
|
135 |
difficulty = gr.Radio(
|
136 |
["Beginner", "Intermediate", "Advanced"],
|
137 |
label="Difficulty Level",
|
138 |
-
|
139 |
)
|
140 |
student_input = gr.Textbox(
|
141 |
placeholder="Type your query here...",
|
142 |
-
label="Your Input"
|
143 |
-
info="Enter the topic you want to learn"
|
144 |
)
|
145 |
submit_button_text = gr.Button("Generate Lesson & Question", variant="primary")
|
146 |
|
@@ -148,8 +175,8 @@ with gr.Blocks() as demo:
|
|
148 |
lesson_output = gr.Markdown(label="Lesson")
|
149 |
question_output = gr.Markdown(label="Comprehension Question")
|
150 |
feedback_output = gr.Markdown(label="Feedback")
|
151 |
-
|
152 |
-
#
|
153 |
with gr.Row():
|
154 |
with gr.Column(scale=2):
|
155 |
model_selector = gr.Radio(
|
@@ -158,58 +185,49 @@ with gr.Blocks() as demo:
|
|
158 |
value="Model 1 (Turbo Realism)"
|
159 |
)
|
160 |
submit_button_visual = gr.Button("Generate Visuals", variant="primary")
|
161 |
-
submit_button_video = gr.Button("Generate Video with Voice", variant="primary")
|
162 |
|
163 |
with gr.Column(scale=3):
|
164 |
output1 = gr.Image(label="Generated Image 1")
|
165 |
output2 = gr.Image(label="Generated Image 2")
|
166 |
output3 = gr.Image(label="Generated Image 3")
|
167 |
-
video_output = gr.Video(label="Generated Video with Voice")
|
168 |
-
|
169 |
gr.Markdown("""
|
170 |
### How to Use
|
171 |
-
1. **Text Section**: Select a subject and difficulty, type your query, and click 'Generate Lesson & Question'
|
172 |
-
2. **Visual Section**:
|
173 |
-
3.
|
174 |
""")
|
175 |
-
|
|
|
176 |
def process_output_text(subject, difficulty, student_input):
|
|
|
177 |
try:
|
178 |
-
|
179 |
-
parsed = eval(tutor_output)
|
180 |
return parsed["lesson"], parsed["question"], parsed["feedback"]
|
181 |
-
except:
|
|
|
182 |
return "Error parsing output", "No question available", "No feedback available"
|
183 |
-
|
184 |
def process_output_visual(text, selected_model):
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
except:
|
189 |
-
return None, None, None
|
190 |
-
|
191 |
def process_output_video(text):
|
192 |
-
|
193 |
-
video_path = generate_text_to_video(text)
|
194 |
-
return video_path
|
195 |
-
except:
|
196 |
-
return None
|
197 |
|
198 |
-
#
|
199 |
submit_button_text.click(
|
200 |
fn=process_output_text,
|
201 |
inputs=[subject, difficulty, student_input],
|
202 |
outputs=[lesson_output, question_output, feedback_output]
|
203 |
)
|
204 |
-
|
205 |
-
# Generate Visual Output
|
206 |
submit_button_visual.click(
|
207 |
fn=process_output_visual,
|
208 |
inputs=[student_input, model_selector],
|
209 |
outputs=[output1, output2, output3]
|
210 |
)
|
211 |
-
|
212 |
-
# Generate Video Output
|
213 |
submit_button_video.click(
|
214 |
fn=process_output_video,
|
215 |
inputs=[student_input],
|
@@ -217,4 +235,5 @@ with gr.Blocks() as demo:
|
|
217 |
)
|
218 |
|
219 |
if __name__ == "__main__":
|
220 |
-
|
|
|
|
2 |
from groq import Groq
|
3 |
import os
|
4 |
import threading
|
|
|
5 |
import tempfile
|
6 |
+
import logging
|
7 |
+
from moviepy.editor import TextClip, concatenate_videoclips, AudioFileClip, ColorClip
|
8 |
+
|
9 |
+
# Set up logging for debugging
|
10 |
+
logging.basicConfig(level=logging.INFO)
|
11 |
+
logger = logging.getLogger(__name__)
|
12 |
|
13 |
+
# Disable proxies to avoid 'proxies' argument error
|
14 |
+
os.environ["HTTP_PROXY"] = ""
|
15 |
+
os.environ["HTTPS_PROXY"] = ""
|
16 |
|
17 |
+
# Initialize Groq client with error handling
|
18 |
+
try:
|
19 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY", ""))
|
20 |
+
logger.info("Groq client initialized successfully with API key: %s", "set" if os.environ.get("GROQ_API_KEY") else "not set")
|
21 |
+
except Exception as e:
|
22 |
+
logger.error("Failed to initialize Groq client: %s", str(e))
|
23 |
+
raise
|
24 |
+
|
25 |
+
# Load Text-to-Image Models (placeholders; adjust based on actual availability)
|
26 |
+
model1 = gr.load("models/prithivMLmods/SD3.5-Turbo-Realism-2.0-LoRA", fallback=None)
|
27 |
+
model2 = gr.load("models/Purz/face-projection", fallback=None)
|
28 |
|
29 |
# Stop event for threading (image generation)
|
30 |
stop_event = threading.Event()
|
31 |
|
32 |
# Function to generate tutor output (lesson, question, feedback)
|
33 |
def generate_tutor_output(subject, difficulty, student_input):
|
34 |
+
if not subject or not difficulty or not student_input:
|
35 |
+
return '{"lesson": "Please provide all inputs.", "question": "", "feedback": ""}'
|
36 |
+
|
37 |
prompt = f"""
|
38 |
You are an expert tutor in {subject} at the {difficulty} level.
|
39 |
The student has provided the following input: "{student_input}"
|
|
|
46 |
Format your response as a JSON object with keys: "lesson", "question", "feedback"
|
47 |
"""
|
48 |
|
49 |
+
try:
|
50 |
+
completion = client.chat.completions.create(
|
51 |
+
messages=[{
|
52 |
+
"role": "system",
|
53 |
+
"content": f"You are the world's best AI tutor, renowned for your ability to explain complex concepts in an engaging, clear, and memorable way and giving math examples. Your expertise in {subject} is unparalleled, and you're adept at tailoring your teaching to {difficulty} level students."
|
54 |
+
}, {
|
55 |
+
"role": "user",
|
56 |
+
"content": prompt,
|
57 |
+
}],
|
58 |
+
model="mixtral-8x7b-32768",
|
59 |
+
max_tokens=1000,
|
60 |
+
)
|
61 |
+
return completion.choices[0].message.content
|
62 |
+
except Exception as e:
|
63 |
+
logger.error("Error in generate_tutor_output: %s", str(e))
|
64 |
+
return '{"lesson": "Error generating lesson.", "question": "", "feedback": ""}'
|
65 |
|
66 |
# Function to generate images based on model selection
|
67 |
def generate_images(text, selected_model):
|
68 |
stop_event.clear()
|
69 |
+
if not text:
|
70 |
+
return ["No text provided."] * 3
|
71 |
|
72 |
if selected_model == "Model 1 (Turbo Realism)":
|
73 |
model = model1
|
|
|
76 |
else:
|
77 |
return ["Invalid model selection."] * 3
|
78 |
|
79 |
+
if model is None:
|
80 |
+
return ["Model not loaded."] * 3
|
81 |
+
|
82 |
results = []
|
83 |
for i in range(3):
|
84 |
if stop_event.is_set():
|
85 |
return ["Image generation stopped by user."] * 3
|
|
|
86 |
modified_text = f"{text} variation {i+1}"
|
87 |
+
try:
|
88 |
+
result = model(modified_text)
|
89 |
+
results.append(result)
|
90 |
+
except Exception as e:
|
91 |
+
logger.error("Error generating image %d: %s", i+1, str(e))
|
92 |
+
results.append(None)
|
93 |
return results
|
94 |
|
95 |
+
# Function to generate text-to-video with voice
|
96 |
def generate_text_to_video(text):
|
97 |
+
if not text:
|
98 |
+
return "No text provided for video generation."
|
99 |
+
|
100 |
try:
|
101 |
# Generate narration using Groq (text-to-speech simulation)
|
102 |
narration_prompt = f"Convert this text to a natural-sounding narration: {text}"
|
|
|
113 |
)
|
114 |
narration_text = narration_response.choices[0].message.content
|
115 |
|
116 |
+
# Simulate TTS with a silent audio clip (replace with real TTS API if available)
|
117 |
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_audio:
|
118 |
+
audio_duration = len(narration_text.split()) / 2 # Rough estimate: 2 words/sec
|
|
|
|
|
119 |
audio = ColorClip(size=(100, 100), color=(0, 0, 0), duration=audio_duration).set_audio(None)
|
120 |
audio.write_audiofile(temp_audio.name, fps=44100, logger=None)
|
121 |
|
|
|
126 |
for i in range(0, len(words), chunk_size):
|
127 |
chunk = " ".join(words[i:i + chunk_size])
|
128 |
clip = TextClip(chunk, fontsize=50, color='white', size=(1280, 720), bg_color='black')
|
129 |
+
clip = clip.set_duration(audio_duration / (len(words) / chunk_size))
|
130 |
clips.append(clip)
|
131 |
|
132 |
# Concatenate clips into a single video
|
|
|
143 |
|
144 |
# Clean up temporary audio file
|
145 |
os.unlink(temp_audio.name)
|
|
|
146 |
return video_path
|
147 |
except Exception as e:
|
148 |
+
logger.error("Error generating video: %s", str(e))
|
149 |
return f"Error generating video: {str(e)}"
|
150 |
|
151 |
+
# Gradio interface setup
|
152 |
+
with gr.Blocks(title="AI Tutor with Visuals") as demo:
|
153 |
+
gr.Markdown("# 🎓 Your AI Tutor with Visuals & Videos")
|
154 |
|
155 |
+
# Text-based output section
|
156 |
with gr.Row():
|
157 |
with gr.Column(scale=2):
|
158 |
subject = gr.Dropdown(
|
159 |
["Math", "Science", "History", "Literature", "Code", "AI"],
|
160 |
label="Subject",
|
161 |
+
value="Math"
|
162 |
)
|
163 |
difficulty = gr.Radio(
|
164 |
["Beginner", "Intermediate", "Advanced"],
|
165 |
label="Difficulty Level",
|
166 |
+
value="Beginner"
|
167 |
)
|
168 |
student_input = gr.Textbox(
|
169 |
placeholder="Type your query here...",
|
170 |
+
label="Your Input"
|
|
|
171 |
)
|
172 |
submit_button_text = gr.Button("Generate Lesson & Question", variant="primary")
|
173 |
|
|
|
175 |
lesson_output = gr.Markdown(label="Lesson")
|
176 |
question_output = gr.Markdown(label="Comprehension Question")
|
177 |
feedback_output = gr.Markdown(label="Feedback")
|
178 |
+
|
179 |
+
# Visual output section
|
180 |
with gr.Row():
|
181 |
with gr.Column(scale=2):
|
182 |
model_selector = gr.Radio(
|
|
|
185 |
value="Model 1 (Turbo Realism)"
|
186 |
)
|
187 |
submit_button_visual = gr.Button("Generate Visuals", variant="primary")
|
188 |
+
submit_button_video = gr.Button("Generate Video with Voice", variant="primary")
|
189 |
|
190 |
with gr.Column(scale=3):
|
191 |
output1 = gr.Image(label="Generated Image 1")
|
192 |
output2 = gr.Image(label="Generated Image 2")
|
193 |
output3 = gr.Image(label="Generated Image 3")
|
194 |
+
video_output = gr.Video(label="Generated Video with Voice")
|
195 |
+
|
196 |
gr.Markdown("""
|
197 |
### How to Use
|
198 |
+
1. **Text Section**: Select a subject and difficulty, type your query, and click 'Generate Lesson & Question'.
|
199 |
+
2. **Visual Section**: Choose an image model and click 'Generate Visuals' for 3 images, or 'Generate Video with Voice' for a narrated video.
|
200 |
+
3. Enjoy your personalized learning experience!
|
201 |
""")
|
202 |
+
|
203 |
+
# Processing functions
|
204 |
def process_output_text(subject, difficulty, student_input):
|
205 |
+
tutor_output = generate_tutor_output(subject, difficulty, student_input)
|
206 |
try:
|
207 |
+
parsed = eval(tutor_output) # Safely parse JSON (consider json.loads in production)
|
|
|
208 |
return parsed["lesson"], parsed["question"], parsed["feedback"]
|
209 |
+
except Exception as e:
|
210 |
+
logger.error("Error parsing tutor output: %s", str(e))
|
211 |
return "Error parsing output", "No question available", "No feedback available"
|
212 |
+
|
213 |
def process_output_visual(text, selected_model):
|
214 |
+
images = generate_images(text, selected_model)
|
215 |
+
return images[0], images[1], images[2]
|
216 |
+
|
|
|
|
|
|
|
217 |
def process_output_video(text):
|
218 |
+
return generate_text_to_video(text)
|
|
|
|
|
|
|
|
|
219 |
|
220 |
+
# Button click handlers
|
221 |
submit_button_text.click(
|
222 |
fn=process_output_text,
|
223 |
inputs=[subject, difficulty, student_input],
|
224 |
outputs=[lesson_output, question_output, feedback_output]
|
225 |
)
|
|
|
|
|
226 |
submit_button_visual.click(
|
227 |
fn=process_output_visual,
|
228 |
inputs=[student_input, model_selector],
|
229 |
outputs=[output1, output2, output3]
|
230 |
)
|
|
|
|
|
231 |
submit_button_video.click(
|
232 |
fn=process_output_video,
|
233 |
inputs=[student_input],
|
|
|
235 |
)
|
236 |
|
237 |
if __name__ == "__main__":
|
238 |
+
# Launch Gradio app
|
239 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, debug=True)
|