Sayiqa commited on
Commit
5a352b6
Β·
verified Β·
1 Parent(s): 7c56c39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +131 -17
app.py CHANGED
@@ -1,17 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_here"
9
- # DeepAI API Key
10
- DEEPAI_API_KEY = "your_deepai_api_key_here"
11
 
12
  # Function to convert speech to text using AssemblyAI API
13
  def speech_to_text(audio_file):
14
- # Upload audio to AssemblyAI for transcription
15
  upload_url = "https://api.assemblyai.com/v2/upload"
16
  headers = {
17
  "authorization": ASSEMBLYAI_API_KEY
@@ -54,25 +165,30 @@ def speech_to_text(audio_file):
54
 
55
  time.sleep(5) # Wait 5 seconds before polling again
56
 
57
- # Function to generate an image based on text using DeepAI's Image Generation API
58
  def generate_image_from_text(text):
59
- image_generation_url = "https://api.deepai.org/api/text2img"
60
  headers = {
61
- "api-key": DEEPAI_API_KEY
62
  }
63
  payload = {
64
- "text": text
 
 
65
  }
66
 
67
- # Request image generation from DeepAI
68
- response = requests.post(image_generation_url, data=payload, headers=headers)
69
 
70
  if response.status_code == 200:
71
- # Get the image URL from the response
72
- image_url = response.json()["output_url"]
73
- return image_url
 
 
 
74
  else:
75
- return "Failed to generate image."
76
 
77
  # Function to download image from URL and return as a PIL image
78
  def get_image_from_url(image_url):
@@ -81,7 +197,7 @@ def get_image_from_url(image_url):
81
  img = Image.open(BytesIO(response.content))
82
  return img
83
  except Exception as e:
84
- return "Error downloading image: " + str(e)
85
 
86
  # Gradio Interface function
87
  def process_audio(audio_file):
@@ -111,5 +227,3 @@ iface = gr.Interface(fn=process_audio,
111
  iface.launch()
112
 
113
 
114
-
115
-
 
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_here"
9
+ # # DeepAI API Key
10
+ # DEEPAI_API_KEY = "your_deepai_api_key_here"
11
+
12
+ # # Function to convert speech to text using AssemblyAI API
13
+ # def speech_to_text(audio_file):
14
+ # # Upload audio to AssemblyAI for transcription
15
+ # upload_url = "https://api.assemblyai.com/v2/upload"
16
+ # headers = {
17
+ # "authorization": ASSEMBLYAI_API_KEY
18
+ # }
19
+
20
+ # # Upload the audio file to AssemblyAI
21
+ # with open(audio_file, 'rb') as file:
22
+ # response = requests.post(upload_url, headers=headers, files={"file": file})
23
+
24
+ # if response.status_code != 200:
25
+ # return "Error uploading audio."
26
+
27
+ # audio_url = response.json()["upload_url"]
28
+
29
+ # # Request transcription from AssemblyAI
30
+ # transcript_url = "https://api.assemblyai.com/v2/transcript"
31
+ # transcript_request = {
32
+ # "audio_url": audio_url
33
+ # }
34
+ # transcript_response = requests.post(transcript_url, json=transcript_request, headers=headers)
35
+
36
+ # if transcript_response.status_code != 200:
37
+ # return "Error requesting transcription."
38
+
39
+ # transcript_id = transcript_response.json()["id"]
40
+
41
+ # # Poll for transcription completion
42
+ # while True:
43
+ # polling_url = f"https://api.assemblyai.com/v2/transcript/{transcript_id}"
44
+ # polling_response = requests.get(polling_url, headers=headers)
45
+
46
+ # if polling_response.status_code != 200:
47
+ # return "Error polling for transcription status."
48
+
49
+ # status = polling_response.json()["status"]
50
+ # if status == "completed":
51
+ # return polling_response.json()["text"]
52
+ # elif status == "failed":
53
+ # return "Transcription failed."
54
+
55
+ # time.sleep(5) # Wait 5 seconds before polling again
56
+
57
+ # # Function to generate an image based on text using DeepAI's Image Generation API
58
+ # def generate_image_from_text(text):
59
+ # image_generation_url = "https://api.deepai.org/api/text2img"
60
+ # headers = {
61
+ # "api-key": DEEPAI_API_KEY
62
+ # }
63
+ # payload = {
64
+ # "text": text
65
+ # }
66
+
67
+ # # Request image generation from DeepAI
68
+ # response = requests.post(image_generation_url, data=payload, headers=headers)
69
+
70
+ # if response.status_code == 200:
71
+ # # Get the image URL from the response
72
+ # image_url = response.json()["output_url"]
73
+ # return image_url
74
+ # else:
75
+ # return "Failed to generate image."
76
+
77
+ # # Function to download image from URL and return as a PIL image
78
+ # def get_image_from_url(image_url):
79
+ # try:
80
+ # response = requests.get(image_url)
81
+ # img = Image.open(BytesIO(response.content))
82
+ # return img
83
+ # except Exception as e:
84
+ # return "Error downloading image: " + str(e)
85
+
86
+ # # Gradio Interface function
87
+ # def process_audio(audio_file):
88
+ # # Convert speech to text
89
+ # text = speech_to_text(audio_file)
90
+ # if text and text != "Error uploading audio." and text != "Error requesting transcription.":
91
+ # print(f"Transcribed text: {text}") # Debug output for transcribed text
92
+
93
+ # # Generate image from the transcribed text
94
+ # image_url = generate_image_from_text(text)
95
+ # if "Failed" not in image_url:
96
+ # print(f"Image URL: {image_url}") # Debug output for image URL
97
+ # # Download the image from URL and return it as a PIL image
98
+ # return get_image_from_url(image_url)
99
+ # else:
100
+ # return image_url
101
+ # else:
102
+ # return "Error processing audio."
103
+
104
+ # # Set up Gradio interface
105
+ # iface = gr.Interface(fn=process_audio,
106
+ # inputs=gr.Audio(type="filepath"), # Audio input
107
+ # outputs=gr.Image(type="pil"), # Image output as PIL image
108
+ # live=True,
109
+ # title="Speech-to-Text to Image Generator")
110
+
111
+ # iface.launch()
112
+
113
+
114
  import gradio as gr
115
  import requests
116
  import time
117
  from PIL import Image
118
  from io import BytesIO
119
 
120
+ # API keys
121
  ASSEMBLYAI_API_KEY = "your_assemblyai_api_key_here"
122
+ STABILITY_AI_API_KEY = "your_stability_ai_api_key_here"
 
123
 
124
  # Function to convert speech to text using AssemblyAI API
125
  def speech_to_text(audio_file):
 
126
  upload_url = "https://api.assemblyai.com/v2/upload"
127
  headers = {
128
  "authorization": ASSEMBLYAI_API_KEY
 
165
 
166
  time.sleep(5) # Wait 5 seconds before polling again
167
 
168
+ # Function to generate an image based on text using Stability AI (Stable Diffusion)
169
  def generate_image_from_text(text):
170
+ image_generation_url = "https://stability.ai/api/v3/generate" # Stability AI API endpoint (assuming)
171
  headers = {
172
+ "Authorization": f"Bearer {STABILITY_AI_API_KEY}"
173
  }
174
  payload = {
175
+ "text": text,
176
+ "width": 512, # Adjust image dimensions as needed
177
+ "height": 512
178
  }
179
 
180
+ # Request image generation from Stability AI
181
+ response = requests.post(image_generation_url, json=payload, headers=headers)
182
 
183
  if response.status_code == 200:
184
+ # Get the image URL from the response (assuming the response contains a URL)
185
+ image_url = response.json().get("image_url", "")
186
+ if image_url:
187
+ return image_url
188
+ else:
189
+ return "Failed to generate image: No image URL found in response."
190
  else:
191
+ return f"Failed to generate image: {response.status_code}"
192
 
193
  # Function to download image from URL and return as a PIL image
194
  def get_image_from_url(image_url):
 
197
  img = Image.open(BytesIO(response.content))
198
  return img
199
  except Exception as e:
200
+ return f"Error downloading image: {str(e)}"
201
 
202
  # Gradio Interface function
203
  def process_audio(audio_file):
 
227
  iface.launch()
228
 
229