Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,7 +19,66 @@ import tempfile
|
|
| 19 |
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
| 20 |
|
| 21 |
# --------- File Extractors ---------
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# --------- Text to Speech Function ---------
|
| 25 |
def speak_auto(text):
|
|
|
|
| 19 |
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
| 20 |
|
| 21 |
# --------- File Extractors ---------
|
| 22 |
+
def extract_text_from_docx(docx_file):
|
| 23 |
+
document = Document(docx_file)
|
| 24 |
+
return "\n".join([para.text for para in document.paragraphs])
|
| 25 |
+
|
| 26 |
+
def extract_text_from_csv(csv_file):
|
| 27 |
+
df = pd.read_csv(csv_file)
|
| 28 |
+
return df.to_string(index=False)
|
| 29 |
+
|
| 30 |
+
def extract_text_from_xlsx(xlsx_file):
|
| 31 |
+
df = pd.read_excel(xlsx_file)
|
| 32 |
+
return df.to_string(index=False)
|
| 33 |
+
|
| 34 |
+
def extract_text_from_pptx(pptx_file):
|
| 35 |
+
prs = Presentation(pptx_file)
|
| 36 |
+
text_runs = []
|
| 37 |
+
for slide in prs.slides:
|
| 38 |
+
for shape in slide.shapes:
|
| 39 |
+
if hasattr(shape, "text"):
|
| 40 |
+
text_runs.append(shape.text)
|
| 41 |
+
return "\n".join(text_runs)
|
| 42 |
+
|
| 43 |
+
def extract_text_from_pdf(pdf_file):
|
| 44 |
+
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
|
| 45 |
+
text = ""
|
| 46 |
+
for page in doc:
|
| 47 |
+
text += page.get_text()
|
| 48 |
+
return text
|
| 49 |
+
|
| 50 |
+
def extract_text_from_html(html_file):
|
| 51 |
+
soup = BeautifulSoup(html_file.read(), "html.parser")
|
| 52 |
+
return soup.get_text()
|
| 53 |
+
|
| 54 |
+
def extract_text_from_tex(tex_file):
|
| 55 |
+
content = tex_file.read().decode("utf-8")
|
| 56 |
+
content = re.sub(r'\\[a-zA-Z]+\{[^}]*\}', '', content)
|
| 57 |
+
content = re.sub(r'\\[a-zA-Z]+', '', content)
|
| 58 |
+
return content
|
| 59 |
+
|
| 60 |
+
# --------- Process Video Function ---------
|
| 61 |
+
def process_video(video_file):
|
| 62 |
+
video_bytes = video_file.read()
|
| 63 |
+
filetype = video_file.name.split(".")[-1].lower()
|
| 64 |
+
mime_map = {
|
| 65 |
+
"avi": "video/x-msvideo",
|
| 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")
|
| 76 |
+
return {
|
| 77 |
+
"inline_data": {
|
| 78 |
+
"mime_type": mime_type,
|
| 79 |
+
"data": encoded_video
|
| 80 |
+
}
|
| 81 |
+
}
|
| 82 |
|
| 83 |
# --------- Text to Speech Function ---------
|
| 84 |
def speak_auto(text):
|