Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,8 @@ from pptx import Presentation
|
|
| 9 |
from reportlab.lib.pagesizes import A4
|
| 10 |
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
| 11 |
from reportlab.lib.styles import getSampleStyleSheet
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Set Gemini API Key
|
| 14 |
os.environ["GEMINI_API_KEY"] = "AIzaSyAQLUStrjXv2_zrAjssapa_sYXQWOqVeiE" # Ensure this is set securely in Hugging Face environment
|
|
@@ -43,6 +45,17 @@ def extract_text_from_pdf(pdf_file):
|
|
| 43 |
text += page.get_text()
|
| 44 |
return text
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
def export_conversation_to_pdf(conversation_history):
|
| 47 |
pdf_path = "conversation_clean.pdf"
|
| 48 |
doc = SimpleDocTemplate(pdf_path, pagesize=A4)
|
|
@@ -69,8 +82,8 @@ def main():
|
|
| 69 |
if "chat_active" not in st.session_state:
|
| 70 |
st.session_state.chat_active = True
|
| 71 |
|
| 72 |
-
uploaded_file = st.file_uploader("Upload a file (.txt, .docx, .csv, .xlsx, .pptx, .pdf):",
|
| 73 |
-
type=["txt", "docx", "csv", "xlsx", "pptx", "pdf"])
|
| 74 |
|
| 75 |
if uploaded_file and not st.session_state.document_text:
|
| 76 |
filetype = uploaded_file.name.split(".")[-1].lower()
|
|
@@ -87,6 +100,10 @@ def main():
|
|
| 87 |
st.session_state.document_text = extract_text_from_pptx(uploaded_file)
|
| 88 |
elif filetype == "pdf":
|
| 89 |
st.session_state.document_text = extract_text_from_pdf(uploaded_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
else:
|
| 91 |
st.error("Unsupported file format.")
|
| 92 |
except Exception as e:
|
|
@@ -112,11 +129,9 @@ def main():
|
|
| 112 |
contents = [
|
| 113 |
types.Content(
|
| 114 |
role="user",
|
| 115 |
-
parts=[
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
types.Part.from_text(text="If needed, search the web for more context.")
|
| 119 |
-
]
|
| 120 |
)
|
| 121 |
]
|
| 122 |
tools = [types.Tool(google_search=types.GoogleSearch())]
|
|
|
|
| 9 |
from reportlab.lib.pagesizes import A4
|
| 10 |
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
| 11 |
from reportlab.lib.styles import getSampleStyleSheet
|
| 12 |
+
from bs4 import BeautifulSoup
|
| 13 |
+
import re
|
| 14 |
|
| 15 |
# Set Gemini API Key
|
| 16 |
os.environ["GEMINI_API_KEY"] = "AIzaSyAQLUStrjXv2_zrAjssapa_sYXQWOqVeiE" # Ensure this is set securely in Hugging Face environment
|
|
|
|
| 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 |
+
# Basic regex to remove LaTeX commands
|
| 55 |
+
content = re.sub(r'\\[a-zA-Z]+\{[^}]*\}', '', content) # Remove LaTeX commands
|
| 56 |
+
content = re.sub(r'\\[a-zA-Z]+', '', content) # Remove LaTeX commands like \section, \textbf, etc.
|
| 57 |
+
return content
|
| 58 |
+
|
| 59 |
def export_conversation_to_pdf(conversation_history):
|
| 60 |
pdf_path = "conversation_clean.pdf"
|
| 61 |
doc = SimpleDocTemplate(pdf_path, pagesize=A4)
|
|
|
|
| 82 |
if "chat_active" not in st.session_state:
|
| 83 |
st.session_state.chat_active = True
|
| 84 |
|
| 85 |
+
uploaded_file = st.file_uploader("Upload a file (.txt, .docx, .csv, .xlsx, .pptx, .pdf, .html, .htm, .tex):",
|
| 86 |
+
type=["txt", "docx", "csv", "xlsx", "pptx", "pdf", "html", "htm", "tex"])
|
| 87 |
|
| 88 |
if uploaded_file and not st.session_state.document_text:
|
| 89 |
filetype = uploaded_file.name.split(".")[-1].lower()
|
|
|
|
| 100 |
st.session_state.document_text = extract_text_from_pptx(uploaded_file)
|
| 101 |
elif filetype == "pdf":
|
| 102 |
st.session_state.document_text = extract_text_from_pdf(uploaded_file)
|
| 103 |
+
elif filetype in ["html", "htm"]:
|
| 104 |
+
st.session_state.document_text = extract_text_from_html(uploaded_file)
|
| 105 |
+
elif filetype == "tex":
|
| 106 |
+
st.session_state.document_text = extract_text_from_tex(uploaded_file)
|
| 107 |
else:
|
| 108 |
st.error("Unsupported file format.")
|
| 109 |
except Exception as e:
|
|
|
|
| 129 |
contents = [
|
| 130 |
types.Content(
|
| 131 |
role="user",
|
| 132 |
+
parts=[types.Part.from_text(text=st.session_state.document_text),
|
| 133 |
+
types.Part.from_text(text=user_input),
|
| 134 |
+
types.Part.from_text(text="If needed, search the web for more context.")]
|
|
|
|
|
|
|
| 135 |
)
|
| 136 |
]
|
| 137 |
tools = [types.Tool(google_search=types.GoogleSearch())]
|