jonloporto commited on
Commit
8d6debd
·
verified ·
1 Parent(s): 4eaa976

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -101
app.py DELETED
@@ -1,101 +0,0 @@
1
- import gradio as gr
2
- from transformers import pipeline
3
- import torch
4
- from diffusers import DiffusionPipeline
5
-
6
- # Load speech-to-text model (Whisper)
7
- transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base")
8
-
9
- # Load image generation model (Stable Diffusion)
10
- device = "cuda" if torch.cuda.is_available() else "cpu"
11
- pipe = DiffusionPipeline.from_pretrained(
12
- "runwayml/stable-diffusion-v1-5",
13
- torch_dtype=torch.float16 if device == "cuda" else torch.float32
14
- )
15
- pipe = pipe.to(device)
16
-
17
- # Speech-to-text function
18
- def transcribe_audio(audio):
19
- """Convert audio to text using Whisper"""
20
- if audio is None:
21
- return ""
22
-
23
- try:
24
- # Handle both file path and numpy array inputs
25
- if isinstance(audio, tuple):
26
- # If audio is a tuple of (sample_rate, audio_data)
27
- sample_rate, audio_data = audio
28
- result = transcriber({"sampling_rate": sample_rate, "raw": audio_data})
29
- else:
30
- # If audio is a file path
31
- result = transcriber(audio, chunk_length_s=30, stride_length_s=5)
32
-
33
- return result.get("text", "")
34
- except Exception as e:
35
- return f"Error transcribing audio: {str(e)}"
36
-
37
- # Image generation function
38
- def generate_image_from_text(prompt):
39
- """Generate an image from a text prompt using Stable Diffusion"""
40
- if not prompt or prompt.strip() == "":
41
- return None, "Please provide a text prompt"
42
-
43
- try:
44
- with torch.no_grad():
45
- image = pipe(prompt, num_inference_steps=50, guidance_scale=7.5).images[0]
46
- return image, f"✓ Generated image from prompt: '{prompt}'"
47
- except Exception as e:
48
- return None, f"Error generating image: {str(e)}"
49
-
50
- # Combined function: speech -> text -> image
51
- def speech_to_image(audio):
52
- """Convert speech to text, then generate image from the text"""
53
- # Step 1: Convert speech to text
54
- text_prompt = transcribe_audio(audio)
55
-
56
- if text_prompt.startswith("Error"):
57
- return None, text_prompt
58
-
59
- # Step 2: Generate image from text
60
- image, status = generate_image_from_text(text_prompt)
61
-
62
- return image, f"Transcript: '{text_prompt}'\n\n{status}"
63
-
64
- # Gradio interface with tabs
65
- with gr.Blocks(title="AI Image Generation from Speech") as demo:
66
- gr.Markdown("# 🎨 AI Image Generation from Speech")
67
- gr.Markdown("Speak your image description, and the AI will generate an image based on your words!")
68
-
69
- with gr.Tab("🎤 Speech to Image"):
70
- gr.Markdown("Record or upload audio with your image description")
71
- audio_input = gr.Audio(label="Record Audio", type="numpy")
72
- generate_btn = gr.Button("Generate Image from Speech", variant="primary")
73
- output_image = gr.Image(label="Generated Image")
74
- output_text = gr.Textbox(label="Status", interactive=False)
75
-
76
- generate_btn.click(
77
- fn=speech_to_image,
78
- inputs=audio_input,
79
- outputs=[output_image, output_text]
80
- )
81
-
82
- with gr.Tab("⌨️ Text to Image"):
83
- gr.Markdown("Or type a description directly")
84
- text_input = gr.Textbox(
85
- label="Enter Image Description",
86
- placeholder="e.g., a beautiful sunset over mountains",
87
- lines=3
88
- )
89
- text_generate_btn = gr.Button("Generate Image", variant="primary")
90
- text_output_image = gr.Image(label="Generated Image")
91
- text_output_status = gr.Textbox(label="Status", interactive=False)
92
-
93
- text_generate_btn.click(
94
- fn=generate_image_from_text,
95
- inputs=text_input,
96
- outputs=[text_output_image, text_output_status]
97
- )
98
-
99
- # Launch the interface
100
- if __name__ == "__main__":
101
- demo.launch()