Spaces:
Runtime error
Runtime error
ziyadsuper2017
commited on
Commit
•
db9bdff
1
Parent(s):
50b3dd7
Update app.py
Browse files
app.py
CHANGED
@@ -55,23 +55,20 @@ selected_model = st.selectbox("Choose a Gemini 1.5 Model:", ["gemini-1.5-flash-l
|
|
55 |
enable_tts = st.checkbox("Enable Text-to-Speech")
|
56 |
|
57 |
# --- Helper Functions ---
|
58 |
-
def
|
59 |
-
|
60 |
-
|
61 |
-
image.save(buffered, format="JPEG")
|
62 |
-
img_str = base64.b64encode(buffered.getvalue()).decode()
|
63 |
-
return img_str
|
64 |
|
65 |
def clear_conversation():
|
66 |
st.session_state['chat_history'] = []
|
67 |
st.session_state['file_uploader_key'] = str(uuid.uuid4())
|
68 |
|
69 |
def display_chat_history():
|
70 |
-
chat_container = st.empty()
|
71 |
-
with chat_container.container():
|
72 |
for entry in st.session_state['chat_history']:
|
73 |
role = entry["role"]
|
74 |
-
parts = entry["parts"][0]
|
75 |
if 'text' in parts:
|
76 |
st.markdown(f"**{role.title()}:** {parts['text']}")
|
77 |
elif 'data' in parts:
|
@@ -85,6 +82,8 @@ def display_chat_history():
|
|
85 |
for page_num in range(len(pdf_reader.pages)):
|
86 |
page = pdf_reader.pages[page_num]
|
87 |
st.write(page.extract_text())
|
|
|
|
|
88 |
elif mime_type.startswith('video'):
|
89 |
st.video(io.BytesIO(base64.b64decode(parts['data'])))
|
90 |
|
@@ -103,28 +102,33 @@ def send_message():
|
|
103 |
if uploaded_files:
|
104 |
for uploaded_file in uploaded_files:
|
105 |
file_content = uploaded_file.read()
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
109 |
|
110 |
# Generate response using the selected model
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
|
|
|
|
|
|
|
|
128 |
|
129 |
st.session_state.user_input = ''
|
130 |
st.session_state.uploaded_files = []
|
@@ -151,8 +155,8 @@ with col2:
|
|
151 |
|
152 |
# --- File Uploader ---
|
153 |
uploaded_files = st.file_uploader(
|
154 |
-
"Upload Files (Images, Videos, PDFs):",
|
155 |
-
type=["png", "jpg", "jpeg", "mp4", "pdf"],
|
156 |
accept_multiple_files=True,
|
157 |
key=st.session_state.file_uploader_key
|
158 |
)
|
|
|
55 |
enable_tts = st.checkbox("Enable Text-to-Speech")
|
56 |
|
57 |
# --- Helper Functions ---
|
58 |
+
def get_file_base64(file_content, mime_type):
|
59 |
+
base64_data = base64.b64encode(file_content).decode()
|
60 |
+
return {"mime_type": mime_type, "data": base64_data}
|
|
|
|
|
|
|
61 |
|
62 |
def clear_conversation():
|
63 |
st.session_state['chat_history'] = []
|
64 |
st.session_state['file_uploader_key'] = str(uuid.uuid4())
|
65 |
|
66 |
def display_chat_history():
|
67 |
+
chat_container = st.empty()
|
68 |
+
with chat_container.container():
|
69 |
for entry in st.session_state['chat_history']:
|
70 |
role = entry["role"]
|
71 |
+
parts = entry["parts"][0]
|
72 |
if 'text' in parts:
|
73 |
st.markdown(f"**{role.title()}:** {parts['text']}")
|
74 |
elif 'data' in parts:
|
|
|
82 |
for page_num in range(len(pdf_reader.pages)):
|
83 |
page = pdf_reader.pages[page_num]
|
84 |
st.write(page.extract_text())
|
85 |
+
elif mime_type.startswith('audio'):
|
86 |
+
st.audio(io.BytesIO(base64.b64decode(parts['data'])), format=mime_type)
|
87 |
elif mime_type.startswith('video'):
|
88 |
st.video(io.BytesIO(base64.b64decode(parts['data'])))
|
89 |
|
|
|
102 |
if uploaded_files:
|
103 |
for uploaded_file in uploaded_files:
|
104 |
file_content = uploaded_file.read()
|
105 |
+
prompt_parts.append(get_file_base64(file_content, uploaded_file.type))
|
106 |
+
st.session_state['chat_history'].append(
|
107 |
+
{"role": "user", "parts": [get_file_base64(file_content, uploaded_file.type)]}
|
108 |
+
)
|
109 |
|
110 |
# Generate response using the selected model
|
111 |
+
try:
|
112 |
+
model = genai.GenerativeModel(
|
113 |
+
model_name=selected_model,
|
114 |
+
generation_config=generation_config,
|
115 |
+
safety_settings=safety_settings
|
116 |
+
)
|
117 |
+
|
118 |
+
response = model.generate_content([{"role": "user", "parts": prompt_parts}])
|
119 |
+
response_text = response.text if hasattr(response, "text") else "No response text found."
|
120 |
+
|
121 |
+
if response_text:
|
122 |
+
st.session_state['chat_history'].append({"role": "model", "parts": [{"text": response_text}]})
|
123 |
+
if enable_tts:
|
124 |
+
tts = gTTS(text=response_text, lang='en')
|
125 |
+
tts_file = BytesIO()
|
126 |
+
tts.write_to_fp(tts_file)
|
127 |
+
tts_file.seek(0)
|
128 |
+
st.audio(tts_file, format='audio/mp3')
|
129 |
+
|
130 |
+
except Exception as e:
|
131 |
+
st.error(f"An error occurred: {e}")
|
132 |
|
133 |
st.session_state.user_input = ''
|
134 |
st.session_state.uploaded_files = []
|
|
|
155 |
|
156 |
# --- File Uploader ---
|
157 |
uploaded_files = st.file_uploader(
|
158 |
+
"Upload Files (Images, Videos, PDFs, MP3):",
|
159 |
+
type=["png", "jpg", "jpeg", "mp4", "pdf", "mp3"],
|
160 |
accept_multiple_files=True,
|
161 |
key=st.session_state.file_uploader_key
|
162 |
)
|