Spaces:
Runtime error
Runtime error
muhammadnasar
commited on
Commit
•
1edd2f3
1
Parent(s):
f57673c
Update app.py
Browse files
app.py
CHANGED
@@ -13,34 +13,38 @@ client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
|
13 |
|
14 |
# Define function to process image description and generate audio
|
15 |
def process_image_and_generate_audio(image_url):
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
32 |
|
33 |
-
|
34 |
-
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
|
43 |
-
|
|
|
|
|
|
|
44 |
|
45 |
# Streamlit UI
|
46 |
def main():
|
@@ -56,16 +60,17 @@ def main():
|
|
56 |
# Generate content and audio
|
57 |
content, audio_response = process_image_and_generate_audio(image_url)
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
|
67 |
-
|
68 |
-
|
69 |
|
70 |
if __name__ == "__main__":
|
71 |
main()
|
|
|
13 |
|
14 |
# Define function to process image description and generate audio
|
15 |
def process_image_and_generate_audio(image_url):
|
16 |
+
try:
|
17 |
+
response = client.chat.completions.create(
|
18 |
+
model="gpt-4o",
|
19 |
+
messages=[
|
20 |
+
{
|
21 |
+
"role": "user",
|
22 |
+
"content": [
|
23 |
+
{"type": "text", "text": "Explain every single thing about this image"},
|
24 |
+
{
|
25 |
+
"type": "image_url",
|
26 |
+
"image_url": {"url": image_url},
|
27 |
+
},
|
28 |
+
],
|
29 |
+
}
|
30 |
+
],
|
31 |
+
max_tokens=300,
|
32 |
+
)
|
33 |
|
34 |
+
# Get content from response
|
35 |
+
content = response.choices[0].message.content
|
36 |
|
37 |
+
# Generate audio from content
|
38 |
+
audio_response = client.audio.speech.create(
|
39 |
+
model="tts-1",
|
40 |
+
voice="alloy",
|
41 |
+
input=content,
|
42 |
+
)
|
43 |
|
44 |
+
return content, audio_response
|
45 |
+
except Exception as e:
|
46 |
+
st.error(f"An error occurred: {str(e)}")
|
47 |
+
return None, None
|
48 |
|
49 |
# Streamlit UI
|
50 |
def main():
|
|
|
60 |
# Generate content and audio
|
61 |
content, audio_response = process_image_and_generate_audio(image_url)
|
62 |
|
63 |
+
if content is not None and audio_response is not None:
|
64 |
+
# Write audio to a temporary file
|
65 |
+
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as f:
|
66 |
+
audio_response.stream_to_file(f.name)
|
67 |
|
68 |
+
# Display content
|
69 |
+
st.markdown("**Description:**")
|
70 |
+
st.write(content)
|
71 |
|
72 |
+
# Display the audio
|
73 |
+
st.audio(open(f.name, "rb").read(), format="audio/mp3")
|
74 |
|
75 |
if __name__ == "__main__":
|
76 |
main()
|