Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,92 +12,28 @@ from reportlab.lib.styles import getSampleStyleSheet
|
|
| 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"))
|
| 18 |
|
| 19 |
# --------- File Extractors ---------
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
for slide in prs.slides:
|
| 36 |
-
for shape in slide.shapes:
|
| 37 |
-
if hasattr(shape, "text"):
|
| 38 |
-
text_runs.append(shape.text)
|
| 39 |
-
return "\n".join(text_runs)
|
| 40 |
-
|
| 41 |
-
def extract_text_from_pdf(pdf_file):
|
| 42 |
-
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
|
| 43 |
-
text = ""
|
| 44 |
-
for page in doc:
|
| 45 |
-
text += page.get_text()
|
| 46 |
-
return text
|
| 47 |
-
|
| 48 |
-
def extract_text_from_html(html_file):
|
| 49 |
-
soup = BeautifulSoup(html_file.read(), "html.parser")
|
| 50 |
-
return soup.get_text()
|
| 51 |
-
|
| 52 |
-
def extract_text_from_tex(tex_file):
|
| 53 |
-
content = tex_file.read().decode("utf-8")
|
| 54 |
-
content = re.sub(r'\\[a-zA-Z]+\{[^}]*\}', '', content)
|
| 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()
|
| 71 |
-
mime_map = {
|
| 72 |
-
"avi": "video/x-msvideo",
|
| 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")
|
| 80 |
-
return {
|
| 81 |
-
"inline_data": {
|
| 82 |
-
"mime_type": mime_type,
|
| 83 |
-
"data": encoded_video
|
| 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,6 +156,9 @@ 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
|
|
|
|
| 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"))
|
| 20 |
|
| 21 |
# --------- File Extractors ---------
|
| 22 |
+
# (Same as before for document extraction functions)
|
| 23 |
+
|
| 24 |
+
# --------- Text to Speech Function ---------
|
| 25 |
+
def speak_auto(text):
|
| 26 |
+
tts = gTTS(text=text, lang='en')
|
| 27 |
+
with tempfile.NamedTemporaryFile(delete=True, suffix=".mp3") as fp:
|
| 28 |
+
tts.save(fp.name)
|
| 29 |
+
audio_bytes = fp.read()
|
| 30 |
+
b64 = base64.b64encode(audio_bytes).decode()
|
| 31 |
+
audio_html = f"""
|
| 32 |
+
<audio autoplay>
|
| 33 |
+
<source src="data:audio/mp3;base64,{b64}" type="audio/mp3">
|
| 34 |
+
</audio>
|
| 35 |
+
"""
|
| 36 |
+
st.markdown(audio_html, unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# --------- Main App ---------
|
| 39 |
def main():
|
|
|
|
| 156 |
st.success("\U0001F4A1 Answer:")
|
| 157 |
st.write(response.text)
|
| 158 |
|
| 159 |
+
# Convert the response text to speech and play it
|
| 160 |
+
speak_auto(response.text)
|
| 161 |
+
|
| 162 |
st.session_state.chat_history.append({
|
| 163 |
"question": user_input,
|
| 164 |
"answer": response.text
|