Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,8 +12,6 @@ from reportlab.lib.styles import getSampleStyleSheet
|
|
| 12 |
from bs4 import BeautifulSoup
|
| 13 |
import re
|
| 14 |
import base64
|
| 15 |
-
from gtts import gTTS
|
| 16 |
-
import tempfile
|
| 17 |
|
| 18 |
# Set Gemini API Key
|
| 19 |
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
|
@@ -57,7 +55,16 @@ def extract_text_from_tex(tex_file):
|
|
| 57 |
content = re.sub(r'\\[a-zA-Z]+', '', content)
|
| 58 |
return content
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
def process_video(video_file):
|
| 62 |
video_bytes = video_file.read()
|
| 63 |
filetype = video_file.name.split(".")[-1].lower()
|
|
@@ -66,10 +73,7 @@ def process_video(video_file):
|
|
| 66 |
"mkv": "video/x-matroska",
|
| 67 |
"flv": "video/x-flv",
|
| 68 |
"wmv": "video/x-ms-wmv",
|
| 69 |
-
"mpeg": "video/mpeg"
|
| 70 |
-
"mp4": "video/mp4",
|
| 71 |
-
"webm": "video/webm",
|
| 72 |
-
"mov": "video/quicktime"
|
| 73 |
}
|
| 74 |
mime_type = mime_map.get(filetype, video_file.type)
|
| 75 |
encoded_video = base64.b64encode(video_bytes).decode("utf-8")
|
|
@@ -80,19 +84,20 @@ def process_video(video_file):
|
|
| 80 |
}
|
| 81 |
}
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
| 96 |
|
| 97 |
# --------- Main App ---------
|
| 98 |
def main():
|
|
@@ -215,9 +220,6 @@ def main():
|
|
| 215 |
st.success("\U0001F4A1 Answer:")
|
| 216 |
st.write(response.text)
|
| 217 |
|
| 218 |
-
# Convert the response text to speech and play it
|
| 219 |
-
speak_auto(response.text)
|
| 220 |
-
|
| 221 |
st.session_state.chat_history.append({
|
| 222 |
"question": user_input,
|
| 223 |
"answer": response.text
|
|
|
|
| 12 |
from bs4 import BeautifulSoup
|
| 13 |
import re
|
| 14 |
import base64
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Set Gemini API Key
|
| 17 |
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
|
|
|
| 55 |
content = re.sub(r'\\[a-zA-Z]+', '', content)
|
| 56 |
return content
|
| 57 |
|
| 58 |
+
def process_image(image_file):
|
| 59 |
+
image_bytes = image_file.read()
|
| 60 |
+
encoded_image = base64.b64encode(image_bytes).decode("utf-8")
|
| 61 |
+
return {
|
| 62 |
+
"inline_data": {
|
| 63 |
+
"mime_type": image_file.type,
|
| 64 |
+
"data": encoded_image
|
| 65 |
+
}
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
def process_video(video_file):
|
| 69 |
video_bytes = video_file.read()
|
| 70 |
filetype = video_file.name.split(".")[-1].lower()
|
|
|
|
| 73 |
"mkv": "video/x-matroska",
|
| 74 |
"flv": "video/x-flv",
|
| 75 |
"wmv": "video/x-ms-wmv",
|
| 76 |
+
"mpeg": "video/mpeg"
|
|
|
|
|
|
|
|
|
|
| 77 |
}
|
| 78 |
mime_type = mime_map.get(filetype, video_file.type)
|
| 79 |
encoded_video = base64.b64encode(video_bytes).decode("utf-8")
|
|
|
|
| 84 |
}
|
| 85 |
}
|
| 86 |
|
| 87 |
+
def export_conversation_to_pdf(conversation_history):
|
| 88 |
+
pdf_path = "Conversation.pdf"
|
| 89 |
+
doc = SimpleDocTemplate(pdf_path, pagesize=A4)
|
| 90 |
+
styles = getSampleStyleSheet()
|
| 91 |
+
elements = []
|
| 92 |
+
|
| 93 |
+
for i, (user_q, gemini_a) in enumerate(conversation_history):
|
| 94 |
+
elements.append(Paragraph(f"<b>Q{i+1}:</b> {user_q}", styles["Normal"]))
|
| 95 |
+
elements.append(Spacer(1, 8))
|
| 96 |
+
elements.append(Paragraph(f"<b>A{i+1}:</b> {gemini_a.replace(chr(10), '<br/>')}", styles["Normal"]))
|
| 97 |
+
elements.append(Spacer(1, 16))
|
| 98 |
+
|
| 99 |
+
doc.build(elements)
|
| 100 |
+
return pdf_path
|
| 101 |
|
| 102 |
# --------- Main App ---------
|
| 103 |
def main():
|
|
|
|
| 220 |
st.success("\U0001F4A1 Answer:")
|
| 221 |
st.write(response.text)
|
| 222 |
|
|
|
|
|
|
|
|
|
|
| 223 |
st.session_state.chat_history.append({
|
| 224 |
"question": user_input,
|
| 225 |
"answer": response.text
|