Sayiqa commited on
Commit
b27f82d
Β·
verified Β·
1 Parent(s): 6b5e201

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -3
app.py CHANGED
@@ -1,6 +1,8 @@
1
  import gradio as gr
2
  import requests
3
  import time
 
 
4
 
5
  # AssemblyAI API Key
6
  ASSEMBLYAI_API_KEY = "YOUR_ASSEMBLYAI_API_KEY"
@@ -67,6 +69,15 @@ def generate_image_from_text(text):
67
  else:
68
  return "Failed to generate image."
69
 
 
 
 
 
 
 
 
 
 
70
  # Gradio Interface function
71
  def process_audio(audio_file):
72
  # Convert speech to text
@@ -74,15 +85,20 @@ def process_audio(audio_file):
74
  if text and text != "Error uploading audio." and text != "Error requesting transcription.":
75
  # Generate image from the transcribed text
76
  image_url = generate_image_from_text(text)
77
- return image_url
 
 
 
 
78
  else:
79
  return "Error processing audio."
80
 
81
  # Set up Gradio interface
82
  iface = gr.Interface(fn=process_audio,
83
- inputs=gr.Audio(type="filepath"), # Changed to 'filepath' for Gradio 3.x
84
- outputs=gr.Image(type="url"),
85
  live=True,
86
  title="Speech-to-Text to Image Generator")
87
 
88
  iface.launch()
 
 
1
  import gradio as gr
2
  import requests
3
  import time
4
+ from PIL import Image
5
+ from io import BytesIO
6
 
7
  # AssemblyAI API Key
8
  ASSEMBLYAI_API_KEY = "YOUR_ASSEMBLYAI_API_KEY"
 
69
  else:
70
  return "Failed to generate image."
71
 
72
+ # Function to download image from URL and return as a PIL image
73
+ def get_image_from_url(image_url):
74
+ try:
75
+ response = requests.get(image_url)
76
+ img = Image.open(BytesIO(response.content))
77
+ return img
78
+ except Exception as e:
79
+ return "Error downloading image: " + str(e)
80
+
81
  # Gradio Interface function
82
  def process_audio(audio_file):
83
  # Convert speech to text
 
85
  if text and text != "Error uploading audio." and text != "Error requesting transcription.":
86
  # Generate image from the transcribed text
87
  image_url = generate_image_from_text(text)
88
+ if "Failed" not in image_url:
89
+ # Download the image from URL and return it as a PIL image
90
+ return get_image_from_url(image_url)
91
+ else:
92
+ return image_url
93
  else:
94
  return "Error processing audio."
95
 
96
  # Set up Gradio interface
97
  iface = gr.Interface(fn=process_audio,
98
+ inputs=gr.Audio(type="filepath"), # Audio input
99
+ outputs=gr.Image(type="pil"), # Image output as PIL image
100
  live=True,
101
  title="Speech-to-Text to Image Generator")
102
 
103
  iface.launch()
104
+