Update app.py
Browse files
app.py
CHANGED
|
@@ -10,7 +10,7 @@ from moviepy.editor import TextClip, concatenate_videoclips, AudioFileClip, Colo
|
|
| 10 |
logging.basicConfig(level=logging.INFO)
|
| 11 |
logger = logging.getLogger(__name__)
|
| 12 |
|
| 13 |
-
# Disable proxies to avoid 'proxies'
|
| 14 |
os.environ["HTTP_PROXY"] = ""
|
| 15 |
os.environ["HTTPS_PROXY"] = ""
|
| 16 |
|
|
@@ -22,9 +22,20 @@ except Exception as e:
|
|
| 22 |
logger.error("Failed to initialize Groq client: %s", str(e))
|
| 23 |
raise
|
| 24 |
|
| 25 |
-
# Load Text-to-Image Models
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# Stop event for threading (image generation)
|
| 30 |
stop_event = threading.Event()
|
|
@@ -77,7 +88,7 @@ def generate_images(text, selected_model):
|
|
| 77 |
return ["Invalid model selection."] * 3
|
| 78 |
|
| 79 |
if model is None:
|
| 80 |
-
return ["
|
| 81 |
|
| 82 |
results = []
|
| 83 |
for i in range(3):
|
|
@@ -98,7 +109,6 @@ def generate_text_to_video(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}"
|
| 103 |
narration_response = client.chat.completions.create(
|
| 104 |
messages=[{
|
|
@@ -113,35 +123,28 @@ def generate_text_to_video(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 |
|
| 122 |
-
# Create video clips from text
|
| 123 |
clips = []
|
| 124 |
words = narration_text.split()
|
| 125 |
-
chunk_size = 10
|
| 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
|
| 133 |
final_video = concatenate_videoclips(clips)
|
| 134 |
-
|
| 135 |
-
# Add audio to video
|
| 136 |
audio_clip = AudioFileClip(temp_audio.name)
|
| 137 |
final_video = final_video.set_audio(audio_clip)
|
| 138 |
|
| 139 |
-
# Save video to temporary file
|
| 140 |
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as temp_video:
|
| 141 |
final_video.write_videofile(temp_video.name, fps=24, logger=None)
|
| 142 |
video_path = temp_video.name
|
| 143 |
|
| 144 |
-
# Clean up temporary audio file
|
| 145 |
os.unlink(temp_audio.name)
|
| 146 |
return video_path
|
| 147 |
except Exception as e:
|
|
@@ -152,7 +155,6 @@ def generate_text_to_video(text):
|
|
| 152 |
with gr.Blocks(title="AI Tutor with Visuals") as demo:
|
| 153 |
gr.Markdown("# 🎓 Your AI Tutor with Visuals & Images")
|
| 154 |
|
| 155 |
-
# Section for generating Text-based output
|
| 156 |
with gr.Row():
|
| 157 |
with gr.Column(scale=2):
|
| 158 |
subject = gr.Dropdown(
|
|
@@ -179,7 +181,6 @@ with gr.Blocks(title="AI Tutor with Visuals") as demo:
|
|
| 179 |
question_output = gr.Markdown(label="Comprehension Question")
|
| 180 |
feedback_output = gr.Markdown(label="Feedback")
|
| 181 |
|
| 182 |
-
# Section for generating Visual output
|
| 183 |
with gr.Row():
|
| 184 |
with gr.Column(scale=2):
|
| 185 |
model_selector = gr.Radio(
|
|
@@ -198,16 +199,15 @@ with gr.Blocks(title="AI Tutor with Visuals") as demo:
|
|
| 198 |
|
| 199 |
gr.Markdown("""
|
| 200 |
### How to Use
|
| 201 |
-
1. **Text Section**: Select a subject and difficulty, type your query, and click 'Generate Lesson & Question'
|
| 202 |
-
2. **Visual Section**: Select the model
|
| 203 |
3. Review the AI-generated content to enhance your learning experience!
|
| 204 |
""")
|
| 205 |
|
| 206 |
-
# Processing functions
|
| 207 |
def process_output_text(subject, difficulty, student_input):
|
| 208 |
try:
|
| 209 |
tutor_output = generate_tutor_output(subject, difficulty, student_input)
|
| 210 |
-
parsed = eval(tutor_output) # Use json.loads in production
|
| 211 |
return parsed["lesson"], parsed["question"], parsed["feedback"]
|
| 212 |
except Exception as e:
|
| 213 |
logger.error("Error parsing tutor output: %s", str(e))
|
|
@@ -229,7 +229,6 @@ with gr.Blocks(title="AI Tutor with Visuals") as demo:
|
|
| 229 |
logger.error("Error in process_output_video: %s", str(e))
|
| 230 |
return None
|
| 231 |
|
| 232 |
-
# Button click handlers
|
| 233 |
submit_button_text.click(
|
| 234 |
fn=process_output_text,
|
| 235 |
inputs=[subject, difficulty, student_input],
|
|
|
|
| 10 |
logging.basicConfig(level=logging.INFO)
|
| 11 |
logger = logging.getLogger(__name__)
|
| 12 |
|
| 13 |
+
# Disable proxies to avoid previous 'proxies' error
|
| 14 |
os.environ["HTTP_PROXY"] = ""
|
| 15 |
os.environ["HTTPS_PROXY"] = ""
|
| 16 |
|
|
|
|
| 22 |
logger.error("Failed to initialize Groq client: %s", str(e))
|
| 23 |
raise
|
| 24 |
|
| 25 |
+
# Load Text-to-Image Models with error handling
|
| 26 |
+
try:
|
| 27 |
+
model1 = gr.load("models/prithivMLmods/SD3.5-Turbo-Realism-2.0-LoRA", fallback=None)
|
| 28 |
+
logger.info("Model 1 loaded successfully: SD3.5-Turbo-Realism-2.0-LoRA")
|
| 29 |
+
except Exception as e:
|
| 30 |
+
logger.error("Failed to load Model 1: %s", str(e))
|
| 31 |
+
model1 = None # Fallback to None if loading fails
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
model2 = gr.load("models/Purz/face-projection", fallback=None)
|
| 35 |
+
logger.info("Model 2 loaded successfully: face-projection")
|
| 36 |
+
except Exception as e:
|
| 37 |
+
logger.error("Failed to load Model 2: %s", str(e))
|
| 38 |
+
model2 = None # Fallback to None if loading fails
|
| 39 |
|
| 40 |
# Stop event for threading (image generation)
|
| 41 |
stop_event = threading.Event()
|
|
|
|
| 88 |
return ["Invalid model selection."] * 3
|
| 89 |
|
| 90 |
if model is None:
|
| 91 |
+
return ["Selected model is not available."] * 3
|
| 92 |
|
| 93 |
results = []
|
| 94 |
for i in range(3):
|
|
|
|
| 109 |
return "No text provided for video generation."
|
| 110 |
|
| 111 |
try:
|
|
|
|
| 112 |
narration_prompt = f"Convert this text to a natural-sounding narration: {text}"
|
| 113 |
narration_response = client.chat.completions.create(
|
| 114 |
messages=[{
|
|
|
|
| 123 |
)
|
| 124 |
narration_text = narration_response.choices[0].message.content
|
| 125 |
|
|
|
|
| 126 |
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_audio:
|
| 127 |
audio_duration = len(narration_text.split()) / 2 # Rough estimate: 2 words/sec
|
| 128 |
audio = ColorClip(size=(100, 100), color=(0, 0, 0), duration=audio_duration).set_audio(None)
|
| 129 |
audio.write_audiofile(temp_audio.name, fps=44100, logger=None)
|
| 130 |
|
|
|
|
| 131 |
clips = []
|
| 132 |
words = narration_text.split()
|
| 133 |
+
chunk_size = 10
|
| 134 |
for i in range(0, len(words), chunk_size):
|
| 135 |
chunk = " ".join(words[i:i + chunk_size])
|
| 136 |
clip = TextClip(chunk, fontsize=50, color='white', size=(1280, 720), bg_color='black')
|
| 137 |
clip = clip.set_duration(audio_duration / (len(words) / chunk_size))
|
| 138 |
clips.append(clip)
|
| 139 |
|
|
|
|
| 140 |
final_video = concatenate_videoclips(clips)
|
|
|
|
|
|
|
| 141 |
audio_clip = AudioFileClip(temp_audio.name)
|
| 142 |
final_video = final_video.set_audio(audio_clip)
|
| 143 |
|
|
|
|
| 144 |
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as temp_video:
|
| 145 |
final_video.write_videofile(temp_video.name, fps=24, logger=None)
|
| 146 |
video_path = temp_video.name
|
| 147 |
|
|
|
|
| 148 |
os.unlink(temp_audio.name)
|
| 149 |
return video_path
|
| 150 |
except Exception as e:
|
|
|
|
| 155 |
with gr.Blocks(title="AI Tutor with Visuals") as demo:
|
| 156 |
gr.Markdown("# 🎓 Your AI Tutor with Visuals & Images")
|
| 157 |
|
|
|
|
| 158 |
with gr.Row():
|
| 159 |
with gr.Column(scale=2):
|
| 160 |
subject = gr.Dropdown(
|
|
|
|
| 181 |
question_output = gr.Markdown(label="Comprehension Question")
|
| 182 |
feedback_output = gr.Markdown(label="Feedback")
|
| 183 |
|
|
|
|
| 184 |
with gr.Row():
|
| 185 |
with gr.Column(scale=2):
|
| 186 |
model_selector = gr.Radio(
|
|
|
|
| 199 |
|
| 200 |
gr.Markdown("""
|
| 201 |
### How to Use
|
| 202 |
+
1. **Text Section**: Select a subject and difficulty, type your query, and click 'Generate Lesson & Question'.
|
| 203 |
+
2. **Visual Section**: Select the model, then click 'Generate Visuals' for 3 images or 'Generate Video with Voice' for a narrated video.
|
| 204 |
3. Review the AI-generated content to enhance your learning experience!
|
| 205 |
""")
|
| 206 |
|
|
|
|
| 207 |
def process_output_text(subject, difficulty, student_input):
|
| 208 |
try:
|
| 209 |
tutor_output = generate_tutor_output(subject, difficulty, student_input)
|
| 210 |
+
parsed = eval(tutor_output) # Use json.loads in production
|
| 211 |
return parsed["lesson"], parsed["question"], parsed["feedback"]
|
| 212 |
except Exception as e:
|
| 213 |
logger.error("Error parsing tutor output: %s", str(e))
|
|
|
|
| 229 |
logger.error("Error in process_output_video: %s", str(e))
|
| 230 |
return None
|
| 231 |
|
|
|
|
| 232 |
submit_button_text.click(
|
| 233 |
fn=process_output_text,
|
| 234 |
inputs=[subject, difficulty, student_input],
|