gnumanth commited on
Commit
0f03e2f
·
verified ·
1 Parent(s): d7c8d35

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -24
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
- def save_uploaded_file(file):
29
- """Saves a file uploaded with gardio to a temporary location."""
 
 
30
 
31
- if file is None:
32
- return None
33
 
34
- try:
35
- temp_dir = tempfile.gettempdir()
36
- temp_filename = tempfile.mkstemp(dir=temp_dir, suffix=os.path.splitext(file.filename)[1])[1]
37
 
38
- # Check if the file object is valid before attempting to read it
39
- if not hasattr(file, 'filename') or not file.filename:
40
- return "Error: Invalid file object. Missing filename."
41
 
 
 
 
 
 
 
 
 
42
 
43
  with open(temp_filename, 'wb') as f:
44
- try:
45
- # Crucial: Check for a 'read' attribute to handle various file-like objects
46
- if hasattr(file, 'read'):
47
- for chunk in file.chunks():
48
- f.write(chunk)
49
- elif hasattr(file, 'getvalue'):
50
- f.write(file.getvalue())
51
- else:
52
- return "Error: Unsupported file-like object. Missing 'read' or 'getvalue'."
53
- except Exception as e:
54
- return f"Error writing to file: {str(e)}"
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: