namberino commited on
Commit
581a346
·
1 Parent(s): 5f9ac06

Fix file upload

Browse files
Files changed (1) hide show
  1. app.py +82 -2
app.py CHANGED
@@ -7,6 +7,86 @@ import io
7
  # In-process client for the FastAPI app
8
  client = TestClient(fastapi_app)
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def call_generate(file_obj, topics, n_questions, difficulty, question_type):
11
  if file_obj is None:
12
  return {"error": "No file uploaded."}
@@ -52,7 +132,7 @@ with gr.Blocks(title="RAG MCQ generator") as gradio_app:
52
  gr.Markdown("## Upload a file and generate MCQs")
53
 
54
  with gr.Row():
55
- file_input = gr.File(label="Upload file (PDF, docx, etc)")
56
  topics = gr.Textbox(label="Topics (comma separated)", placeholder="e.g. calculus, derivatives")
57
  with gr.Row():
58
  n_questions = gr.Slider(minimum=1, maximum=50, step=1, value=5, label="Number of questions")
@@ -71,5 +151,5 @@ with gr.Blocks(title="RAG MCQ generator") as gradio_app:
71
  app = gradio_app
72
 
73
  if __name__ == "__main__":
74
- # demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
75
  gradio_app.launch()
 
7
  # In-process client for the FastAPI app
8
  client = TestClient(fastapi_app)
9
 
10
+ def read_file_input(file_obj):
11
+ if file_obj is None:
12
+ return None
13
+
14
+ # file-like object (has read)
15
+ if hasattr(file_obj, "read"):
16
+ try:
17
+ file_obj.seek(0)
18
+ except Exception:
19
+ pass
20
+ try:
21
+ data = file_obj.read()
22
+ # If read returns str (rare), encode it
23
+ if isinstance(data, str):
24
+ return data.encode()
25
+ return data
26
+ except Exception:
27
+ # continue to other strategies
28
+ pass
29
+
30
+ # raw bytes
31
+ if isinstance(file_obj, (bytes, bytearray)):
32
+ return bytes(file_obj)
33
+
34
+ # path string (local path)
35
+ if isinstance(file_obj, str):
36
+ try:
37
+ with open(file_obj, "rb") as f:
38
+ return f.read()
39
+ except Exception:
40
+ # not a path, fall through to try encoding string
41
+ return file_obj.encode()
42
+
43
+ # dict-like (old Gradio or different frontends)
44
+ try:
45
+ if isinstance(file_obj, dict):
46
+ # common keys: "name", "data"
47
+ if "data" in file_obj:
48
+ data = file_obj["data"]
49
+ if isinstance(data, (bytes, bytearray)):
50
+ return bytes(data)
51
+ if isinstance(data, str):
52
+ return data.encode()
53
+ if "name" in file_obj:
54
+ maybe_path = file_obj["name"]
55
+ if isinstance(maybe_path, str):
56
+ try:
57
+ with open(maybe_path, "rb") as f:
58
+ return f.read()
59
+ except Exception:
60
+ pass
61
+ except Exception:
62
+ pass
63
+
64
+ # Object with attributes (NamedString with .name/.value)
65
+ try:
66
+ name = getattr(file_obj, "name", None)
67
+ data = getattr(file_obj, "data", None)
68
+ value = getattr(file_obj, "value", None)
69
+ if isinstance(data, (bytes, bytearray)):
70
+ return bytes(data)
71
+ if isinstance(value, (bytes, bytearray)):
72
+ return bytes(value)
73
+ if isinstance(value, str):
74
+ return value.encode()
75
+ if isinstance(name, str):
76
+ try:
77
+ with open(name, "rb") as f:
78
+ return f.read()
79
+ except Exception:
80
+ pass
81
+ except Exception:
82
+ pass
83
+
84
+ # String representation encoded
85
+ try:
86
+ return str(file_obj).encode()
87
+ except Exception:
88
+ return None
89
+
90
  def call_generate(file_obj, topics, n_questions, difficulty, question_type):
91
  if file_obj is None:
92
  return {"error": "No file uploaded."}
 
132
  gr.Markdown("## Upload a file and generate MCQs")
133
 
134
  with gr.Row():
135
+ file_input = gr.File(label="Upload file (PDF, docx, etc)", type="file")
136
  topics = gr.Textbox(label="Topics (comma separated)", placeholder="e.g. calculus, derivatives")
137
  with gr.Row():
138
  n_questions = gr.Slider(minimum=1, maximum=50, step=1, value=5, label="Number of questions")
 
151
  app = gradio_app
152
 
153
  if __name__ == "__main__":
154
+ # gradio_app.launch(server_name="0.0.0.0", server_port=7860, share=False)
155
  gradio_app.launch()