Sayandip commited on
Commit
da0bedf
·
verified ·
1 Parent(s): 8549931

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -6
app.py CHANGED
@@ -6,7 +6,7 @@ import openpyxl
6
  from pdfminer.high_level import extract_text
7
  import csv
8
  from pptx import Presentation
9
- from io import StringIO
10
  import google.generativeai as genai
11
 
12
  # Setup
@@ -66,6 +66,9 @@ uploaded_file = st.file_uploader("Upload a document or image", type=["docx", "xl
66
  # Question input
67
  question = st.text_input("💬 Enter your question:")
68
 
 
 
 
69
  # Processing
70
  if uploaded_file and question:
71
  file_ext = uploaded_file.name.split('.')[-1].lower()
@@ -85,8 +88,17 @@ if uploaded_file and question:
85
  {"text": question}
86
  ]
87
  response = model.generate_content(contents=content)
88
- st.success("💡 Answer:")
89
- st.write(response.text)
 
 
 
 
 
 
 
 
 
90
 
91
  except Exception as e:
92
  st.error(f"Error processing image: {e}")
@@ -107,7 +119,17 @@ if uploaded_file and question:
107
  try:
108
  prompt = f"Context: {extracted_text}\n\nQuestion: {question}\n\nAnswer:"
109
  response = model.generate_content(prompt)
110
- st.success("💡 Answer:")
111
- st.write(response.text)
 
 
 
 
 
 
 
 
 
 
112
  except Exception as e:
113
- st.error(f"Error generating answer: {e}")
 
6
  from pdfminer.high_level import extract_text
7
  import csv
8
  from pptx import Presentation
9
+ from io import BytesIO
10
  import google.generativeai as genai
11
 
12
  # Setup
 
66
  # Question input
67
  question = st.text_input("💬 Enter your question:")
68
 
69
+ # Output type selection
70
+ output_type = st.radio("Choose output type", ("Text", "Audio", "Image", "Both"))
71
+
72
  # Processing
73
  if uploaded_file and question:
74
  file_ext = uploaded_file.name.split('.')[-1].lower()
 
88
  {"text": question}
89
  ]
90
  response = model.generate_content(contents=content)
91
+ if output_type in ["Text", "Both"]:
92
+ st.success("💡 Answer:")
93
+ st.write(response.text)
94
+ if output_type in ["Audio", "Both"]:
95
+ audio_response = model.generate_audio(response.text)
96
+ audio_data = base64.b64decode(audio_response.audio_data)
97
+ st.audio(audio_data, format="audio/wav")
98
+ if output_type in ["Image", "Both"]:
99
+ image_response = model.generate_image(response.text)
100
+ image_data = base64.b64decode(image_response.image_data)
101
+ st.image(image_data, caption="Generated Image", use_column_width=True)
102
 
103
  except Exception as e:
104
  st.error(f"Error processing image: {e}")
 
119
  try:
120
  prompt = f"Context: {extracted_text}\n\nQuestion: {question}\n\nAnswer:"
121
  response = model.generate_content(prompt)
122
+ if output_type in ["Text", "Both"]:
123
+ st.success("💡 Answer:")
124
+ st.write(response.text)
125
+ if output_type in ["Audio", "Both"]:
126
+ audio_response = model.generate_audio(response.text)
127
+ audio_data = base64.b64decode(audio_response.audio_data)
128
+ st.audio(audio_data, format="audio/wav")
129
+ if output_type in ["Image", "Both"]:
130
+ image_response = model.generate_image(response.text)
131
+ image_data = base64.b64decode(image_response.image_data)
132
+ st.image(image_data, caption="Generated Image", use_column_width=True)
133
+
134
  except Exception as e:
135
+ st.error(f"Error generating answer: {e}")