ShebMichel commited on
Commit
1a5a87f
Β·
verified Β·
1 Parent(s): 7fff487

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -46
app.py CHANGED
@@ -64,39 +64,12 @@ def analyze_response(text_input, file_input):
64
 
65
 
66
 
67
- # Set up Gradio Interface
68
- # iface = gr.Interface(
69
- # #fn=chatbot,
70
- # inputs=[
71
- # gr.File(label="Upload a file (PDF, JSON, DOCX)"),
72
- # gr.Textbox(label="Your Message"),
73
- # "state" # Keeps chat history between turns
74
- # ],
75
- # outputs="chatbot",
76
- # live=True,
77
- # description="Drag and drop a file and start chatting with the bot based on its contents."
78
- # )
79
- # iface.launch()
80
- #title="πŸ‘‹ Hola-Hello-Bonjour-Mbote-δ½ ε₯½ πŸ‘‹ <br><br> I am geobot-teacher the student marker! <br><br> I am here to analyse each question to determine whether the response qualifies as a pass or fail. <br><br> Try me :)",
81
- #description="Synthetic QA pairs (~1k) was finetuned on top of Gemma_2b_en.")
82
- # inputs="text"
83
-
84
- # iface = gr.Interface(launch,
85
- # inputs="text",
86
- # outputs="text",
87
- # css=css,
88
- # title="πŸ‘‹ Hi, I am geobot-teacher: The Student Marker πŸ‘‹",
89
- # description="Hola/Hello/Bonjour/Mbote/δ½ ε₯½ \n\n"
90
- # "I am here to analyse each question to determine whether the response qualifies as a pass or fail.\n\n"
91
- # "Try me :)",
92
- # )
93
-
94
  # import gradio as gr
95
  # import json
96
  # import fitz # PyMuPDF for PDFs
97
  # from docx import Document
98
 
99
- # Function to read PDF, JSON, or DOCX files and categorize content
100
  # def read_pdf(file):
101
  # text = ""
102
  # pdf = fitz.open(file)
@@ -116,18 +89,20 @@ def read_json(file):
116
  # return text
117
 
118
  def parse_content(content):
119
- # Assuming header and multiple_choice_questions sections are clearly marked in the content
120
- sections = {"header": "", "multiple_choice_questions": ""}
121
- # Here, a simple splitting example (customize this based on your format)
122
- if "header" in content:
123
- sections["header"] = content.split("header")[1].split("header")[0].strip()
124
- if "multiple_choice_questions" in content:
125
- sections["multiple_choice_questions"] = content.split("multiple_choice_questions")[1].strip()
 
 
126
  return sections
127
 
128
  def handle_file_upload(file):
129
  if file is None:
130
- return "Please upload a file.", [], ""
131
 
132
  file_type = file.name.split('.')[-1].lower()
133
  if file_type == 'pdf':
@@ -137,37 +112,144 @@ def handle_file_upload(file):
137
  elif file_type == 'docx':
138
  content = read_docx(file)
139
  else:
140
- return "Unsupported file format. Please upload a PDF, JSON, or DOCX file.", [], ""
141
 
142
- # Parse content into sections
143
  sections = parse_content(content)
144
- options = list(sections.keys()) # Radio button options (e.g., QCM, DTM)
145
 
146
- # Default to showing the first option if available
147
- default_display = sections[options[0]] if options else "No content available"
148
 
149
- return "File uploaded successfully!", options, default_display
 
150
 
151
  def display_selected_content(selection, sections):
 
152
  return sections.get(selection, "No content available")
153
 
154
  # Define the Gradio app
155
  with gr.Blocks() as demo:
156
- gr.Markdown("### Upload a file and select a section (QCM, DTM)")
157
 
158
  # File upload and output elements
159
  file_input = gr.File(label="Upload a file (PDF, JSON, DOCX)")
160
  upload_output = gr.Textbox(label="Upload Status")
161
- radio_button = gr.Radio(label="Select Section", interactive=True)
162
  section_output = gr.Textbox(label="Selected Content")
163
 
164
- # Handle file upload and section selection
165
- file_input.change(fn=handle_file_upload, inputs=file_input, outputs=[upload_output, radio_button, section_output])
166
  radio_button.change(fn=display_selected_content, inputs=[radio_button, radio_button], outputs=section_output)
167
 
168
  # Launch the app
169
  demo.launch()
170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
  # iface = gr.Interface(fn=launch,
172
  # inputs=[
173
  # gr.File(label="Upload a file (PDF, JSON, DOCX)"),gr.Textbox(label="Enter your response"),
 
64
 
65
 
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  # import gradio as gr
68
  # import json
69
  # import fitz # PyMuPDF for PDFs
70
  # from docx import Document
71
 
72
+ # Functions to read different file types
73
  # def read_pdf(file):
74
  # text = ""
75
  # pdf = fitz.open(file)
 
89
  # return text
90
 
91
  def parse_content(content):
92
+ # Assuming QCM, short_answer_questions, and long_answer_questions are sections
93
+ sections = {"QCM": "", "short_answer_questions": "", "long_answer_questions": ""}
94
+ # Simple splitting based on section keywords (customize based on format)
95
+ if "QCM" in content:
96
+ sections["QCM"] = content.split("QCM")[1].split("short_answer_questions")[0].strip()
97
+ if "short_answer_questions" in content:
98
+ sections["short_answer_questions"] = content.split("short_answer_questions")[1].split("long_answer_questions")[0].strip()
99
+ if "long_answer_questions" in content:
100
+ sections["long_answer_questions"] = content.split("long_answer_questions")[1].strip()
101
  return sections
102
 
103
  def handle_file_upload(file):
104
  if file is None:
105
+ return "Please upload a file.", "", gr.update(visible=False, interactive=False)
106
 
107
  file_type = file.name.split('.')[-1].lower()
108
  if file_type == 'pdf':
 
112
  elif file_type == 'docx':
113
  content = read_docx(file)
114
  else:
115
+ return "Unsupported file format. Please upload a PDF, JSON, or DOCX file.", "", gr.update(visible=False, interactive=False)
116
 
117
+ # Parse content into predefined sections
118
  sections = parse_content(content)
 
119
 
120
+ # Display default section content
121
+ default_display = sections["QCM"] # Default to QCM section if it exists
122
 
123
+ # Enable radio button after file upload
124
+ return "File uploaded successfully!", default_display, gr.update(visible=True, interactive=True, choices=["QCM", "short_answer_questions", "long_answer_questions"])
125
 
126
  def display_selected_content(selection, sections):
127
+ # Return content for selected section
128
  return sections.get(selection, "No content available")
129
 
130
  # Define the Gradio app
131
  with gr.Blocks() as demo:
132
+ gr.Markdown("### Upload a file and select a section (QCM, short_answer_questions, long_answer_questions)")
133
 
134
  # File upload and output elements
135
  file_input = gr.File(label="Upload a file (PDF, JSON, DOCX)")
136
  upload_output = gr.Textbox(label="Upload Status")
137
+ radio_button = gr.Radio(["QCM", "short_answer_questions", "long_answer_questions"], label="Select Section", interactive=False, visible=False)
138
  section_output = gr.Textbox(label="Selected Content")
139
 
140
+ # Handle file upload and enable radio button with section selection
141
+ file_input.change(fn=handle_file_upload, inputs=file_input, outputs=[upload_output, section_output, radio_button])
142
  radio_button.change(fn=display_selected_content, inputs=[radio_button, radio_button], outputs=section_output)
143
 
144
  # Launch the app
145
  demo.launch()
146
 
147
+
148
+
149
+ # Set up Gradio Interface
150
+ # iface = gr.Interface(
151
+ # #fn=chatbot,
152
+ # inputs=[
153
+ # gr.File(label="Upload a file (PDF, JSON, DOCX)"),
154
+ # gr.Textbox(label="Your Message"),
155
+ # "state" # Keeps chat history between turns
156
+ # ],
157
+ # outputs="chatbot",
158
+ # live=True,
159
+ # description="Drag and drop a file and start chatting with the bot based on its contents."
160
+ # )
161
+ # iface.launch()
162
+ #title="πŸ‘‹ Hola-Hello-Bonjour-Mbote-δ½ ε₯½ πŸ‘‹ <br><br> I am geobot-teacher the student marker! <br><br> I am here to analyse each question to determine whether the response qualifies as a pass or fail. <br><br> Try me :)",
163
+ #description="Synthetic QA pairs (~1k) was finetuned on top of Gemma_2b_en.")
164
+ # inputs="text"
165
+
166
+ # iface = gr.Interface(launch,
167
+ # inputs="text",
168
+ # outputs="text",
169
+ # css=css,
170
+ # title="πŸ‘‹ Hi, I am geobot-teacher: The Student Marker πŸ‘‹",
171
+ # description="Hola/Hello/Bonjour/Mbote/δ½ ε₯½ \n\n"
172
+ # "I am here to analyse each question to determine whether the response qualifies as a pass or fail.\n\n"
173
+ # "Try me :)",
174
+ # )
175
+
176
+ # import gradio as gr
177
+ # import json
178
+ # import fitz # PyMuPDF for PDFs
179
+ # from docx import Document
180
+
181
+ # Function to read PDF, JSON, or DOCX files and categorize content
182
+ # def read_pdf(file):
183
+ # text = ""
184
+ # pdf = fitz.open(file)
185
+ # for page in pdf:
186
+ # text += page.get_text()
187
+ # pdf.close()
188
+ # return text
189
+
190
+ # def read_json(file):
191
+ # with open(file.name, 'r', encoding='utf-8') as f:
192
+ # data = json.load(f)
193
+ # return json.dumps(data, indent=4)
194
+
195
+ # # def read_docx(file):
196
+ # # doc = Document(file)
197
+ # # text = "\n".join([paragraph.text for paragraph in doc.paragraphs])
198
+ # # return text
199
+
200
+ # def parse_content(content):
201
+ # # Assuming header and multiple_choice_questions sections are clearly marked in the content
202
+ # sections = {"header": "", "multiple_choice_questions": ""}
203
+ # # Here, a simple splitting example (customize this based on your format)
204
+ # if "header" in content:
205
+ # sections["header"] = content.split("header")[1].split("header")[0].strip()
206
+ # if "multiple_choice_questions" in content:
207
+ # sections["multiple_choice_questions"] = content.split("multiple_choice_questions")[1].strip()
208
+ # return sections
209
+
210
+ # def handle_file_upload(file):
211
+ # if file is None:
212
+ # return "Please upload a file.", [], ""
213
+
214
+ # file_type = file.name.split('.')[-1].lower()
215
+ # if file_type == 'pdf':
216
+ # content = read_pdf(file.name)
217
+ # elif file_type == 'json':
218
+ # content = read_json(file)
219
+ # elif file_type == 'docx':
220
+ # content = read_docx(file)
221
+ # else:
222
+ # return "Unsupported file format. Please upload a PDF, JSON, or DOCX file.", [], ""
223
+
224
+ # # Parse content into sections
225
+ # sections = parse_content(content)
226
+ # options = list(sections.keys()) # Radio button options (e.g., QCM, DTM)
227
+
228
+ # # Default to showing the first option if available
229
+ # default_display = sections[options[0]] if options else "No content available"
230
+
231
+ # return "File uploaded successfully!", options, default_display
232
+
233
+ # def display_selected_content(selection, sections):
234
+ # return sections.get(selection, "No content available")
235
+
236
+ # # Define the Gradio app
237
+ # with gr.Blocks() as demo:
238
+ # gr.Markdown("### Upload a file and select a section (QCM, DTM)")
239
+
240
+ # # File upload and output elements
241
+ # file_input = gr.File(label="Upload a file (PDF, JSON, DOCX)")
242
+ # upload_output = gr.Textbox(label="Upload Status")
243
+ # radio_button = gr.Radio(label="Select Section", interactive=True)
244
+ # section_output = gr.Textbox(label="Selected Content")
245
+
246
+ # # Handle file upload and section selection
247
+ # file_input.change(fn=handle_file_upload, inputs=file_input, outputs=[upload_output, radio_button, section_output])
248
+ # radio_button.change(fn=display_selected_content, inputs=[radio_button, radio_button], outputs=section_output)
249
+
250
+ # # Launch the app
251
+ # demo.launch()
252
+
253
  # iface = gr.Interface(fn=launch,
254
  # inputs=[
255
  # gr.File(label="Upload a file (PDF, JSON, DOCX)"),gr.Textbox(label="Enter your response"),