muhammadnasar commited on
Commit
1edd2f3
1 Parent(s): f57673c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -33
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
- response = client.chat.completions.create(
17
- model="gpt-4o",
18
- messages=[
19
- {
20
- "role": "user",
21
- "content": [
22
- {"type": "text", "text": "Explain every single thing about this image"},
23
- {
24
- "type": "image_url",
25
- "image_url": {"url": image_url},
26
- },
27
- ],
28
- }
29
- ],
30
- max_tokens=300,
31
- )
 
32
 
33
- # Get content from response
34
- content = response.choices[0].message.content
35
 
36
- # Generate audio from content
37
- audio_response = client.audio.speech.create(
38
- model="tts-1",
39
- voice="alloy",
40
- input=content,
41
- )
42
 
43
- return content, audio_response
 
 
 
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
- # Write audio to a temporary file
60
- with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as f:
61
- audio_response.stream_to_file(f.name)
 
62
 
63
- # Display content
64
- st.markdown("**Description:**")
65
- st.write(content)
66
 
67
- # Display the audio
68
- st.audio(open(f.name, "rb").read(), format="audio/mp3")
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()