Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
from markitdown import MarkItDown
|
| 4 |
import google.generativeai as genai
|
|
@@ -25,38 +24,48 @@ import tempfile
|
|
| 25 |
import os
|
| 26 |
import io
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
temp_filename = tempfile.mkstemp(dir=temp_dir, suffix=os.path.splitext(file.filename)[1])[1]
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
with open(temp_filename, 'wb') as f:
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
|
| 56 |
return temp_filename
|
| 57 |
-
except Exception as e:
|
| 58 |
-
return f"Error saving file: {str(e)}"
|
| 59 |
|
|
|
|
|
|
|
|
|
|
| 60 |
async def summarize_text(text):
|
| 61 |
"""Summarize the input text using Gemini AI"""
|
| 62 |
try:
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from markitdown import MarkItDown
|
| 3 |
import google.generativeai as genai
|
|
|
|
| 24 |
import os
|
| 25 |
import io
|
| 26 |
|
| 27 |
+
import tempfile
|
| 28 |
+
import os
|
| 29 |
+
import io
|
| 30 |
+
from gardio import upload_file
|
| 31 |
|
| 32 |
+
def save_uploaded_file(file_data):
|
| 33 |
+
"""Saves a file uploaded with gardio to a temporary location.
|
| 34 |
|
| 35 |
+
Args:
|
| 36 |
+
file_data: The file data object returned by gardio.get_file().
|
|
|
|
| 37 |
|
| 38 |
+
Returns:
|
| 39 |
+
The path to the saved file, or an error message as a string.
|
| 40 |
+
"""
|
| 41 |
|
| 42 |
+
if file_data is None:
|
| 43 |
+
return "No file uploaded."
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
# Extract the filename; crucial to avoid errors
|
| 47 |
+
filename = file_data.filename if hasattr(file_data, 'filename') else "unknown_file"
|
| 48 |
+
temp_dir = tempfile.gettempdir()
|
| 49 |
+
temp_filename = tempfile.mkstemp(dir=temp_dir, suffix=os.path.splitext(filename)[1])[1]
|
| 50 |
|
| 51 |
with open(temp_filename, 'wb') as f:
|
| 52 |
+
try:
|
| 53 |
+
if hasattr(file_data, 'read'):
|
| 54 |
+
for chunk in file_data.chunks():
|
| 55 |
+
f.write(chunk)
|
| 56 |
+
elif hasattr(file_data, 'getvalue'):
|
| 57 |
+
f.write(file_data.getvalue())
|
| 58 |
+
else:
|
| 59 |
+
return "Error: Unsupported file-like object."
|
| 60 |
+
|
| 61 |
+
except Exception as e:
|
| 62 |
+
return f"Error writing to file: {str(e)}"
|
| 63 |
|
| 64 |
return temp_filename
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return f"An error occurred during file saving: {str(e)}"
|
| 68 |
+
|
| 69 |
async def summarize_text(text):
|
| 70 |
"""Summarize the input text using Gemini AI"""
|
| 71 |
try:
|