ankban commited on
Commit
ea46440
Β·
verified Β·
1 Parent(s): 5028943

Update file_viewer_panel.py

Browse files
Files changed (1) hide show
  1. file_viewer_panel.py +51 -35
file_viewer_panel.py CHANGED
@@ -1,53 +1,69 @@
1
  import gradio as gr
2
- import os
3
- import json
4
  from file_utils import extract_text_from_file, call_llm
 
5
 
6
  def file_viewer_dashboard():
7
- with gr.Column(scale=4, elem_id="main-column") as file_panel:
8
- file_upload = gr.File(label="πŸ“‚ Upload a File", file_types=[".pdf", ".txt", ".md", ".pptx"])
9
- preview = gr.Textbox(label="πŸ“„ File Content Preview", lines=10, interactive=False)
10
- chat_input = gr.Textbox(label="πŸ’¬ Ask about the file")
11
- chat_output = gr.Chatbot()
12
- quiz_box = gr.Column(visible=False)
13
- quiz_score = gr.Textbox(label="Your Score", interactive=False, visible=False)
 
 
 
14
 
15
  file_text_state = gr.State("")
16
 
17
- def load_and_preview(file):
18
- if not file: return "", ""
 
 
 
19
  text = extract_text_from_file(file)
20
- return text, text
21
 
22
- def handle_chat(query, text):
23
- prompt = f"Using the document below, answer this question:\n\n{text}\n\nQuestion: {query}"
24
- return [(query, call_llm(prompt))]
 
 
 
 
 
 
 
 
 
25
 
26
  def generate_quiz(text):
27
- prompt = f"Create 5 multiple-choice questions from this text. Return as JSON list."
28
- response = call_llm(prompt)
 
 
29
  try:
30
- questions = json.loads(response)
31
- score_state = {"score": 0, "total": len(questions)}
32
- elements = []
 
 
 
 
 
33
 
34
- for i, q in enumerate(questions):
35
- q_md = gr.Markdown(f"**Q{i+1}. {q['question']}**")
36
- q_radio = gr.Radio(q["options"], label="Select an answer")
37
- q_feedback = gr.Textbox(label="Result", interactive=False)
38
 
39
- def grade(choice, correct=q["answer"]):
40
- return "βœ… Correct!" if choice == correct else f"❌ Correct: {correct}"
41
 
42
- q_radio.change(fn=grade, inputs=[q_radio], outputs=[q_feedback])
43
- elements.extend([q_md, q_radio, q_feedback])
44
 
45
- return gr.update(visible=True), elements, gr.update(visible=True, value=f"Score: 0/{score_state['total']}")
46
- except:
47
- return gr.update(visible=False), [], gr.update(visible=True, value="Error generating quiz.")
48
 
49
- file_upload.change(fn=load_and_preview, inputs=[file_upload], outputs=[preview, file_text_state])
50
- chat_input.submit(fn=handle_chat, inputs=[chat_input, file_text_state], outputs=[chat_output])
51
- gr.Button("🧠 Generate Quiz").click(fn=generate_quiz, inputs=[file_text_state], outputs=[quiz_box, quiz_box, quiz_score])
52
 
53
- return file_panel
 
1
  import gradio as gr
 
 
2
  from file_utils import extract_text_from_file, call_llm
3
+ import os, json
4
 
5
  def file_viewer_dashboard():
6
+ with gr.Row():
7
+ with gr.Column(scale=3): # Main viewer
8
+ file_upload = gr.File(label="πŸ“‚ Upload a File", file_types=[".pdf", ".txt", ".md", ".pptx"])
9
+ file_viewer = gr.HTML(label="πŸ“„ File Preview")
10
+
11
+ with gr.Column(scale=2): # Right: Chat + Quiz
12
+ generate_quiz_btn = gr.Button("🧠 Practice Questions")
13
+ quiz_container = gr.Column(visible=False)
14
+ chat_output = gr.Chatbot(label="Ask a question to learn more about the file")
15
+ chat_input = gr.Textbox(label="", placeholder="Ask ChatEDU a question...")
16
 
17
  file_text_state = gr.State("")
18
 
19
+ def render_file(file):
20
+ if not file:
21
+ return "", ""
22
+ ext = os.path.splitext(file.name)[1].lower()
23
+ file_path = file.name
24
  text = extract_text_from_file(file)
 
25
 
26
+ if ext == ".pdf":
27
+ viewer_html = f"<iframe src='/file={file_path}' width='100%' height='700px' style='border:none; border-radius:8px;'></iframe>"
28
+ elif ext in [".txt", ".md", ".pptx"]:
29
+ viewer_html = f"<pre style='padding:12px; background:#f9f9f9;'>{text}</pre>"
30
+ else:
31
+ viewer_html = "<p>Unsupported file type.</p>"
32
+
33
+ return viewer_html, text
34
+
35
+ def ask_question(q, text):
36
+ prompt = f"Answer the following question based only on this document:\n\n{text}\n\nQuestion: {q}"
37
+ return [(q, call_llm(prompt))]
38
 
39
  def generate_quiz(text):
40
+ prompt = (
41
+ "Generate 5 multiple choice questions from this document. "
42
+ "Return a JSON array of questions, each with:\n- question\n- options (list)\n- answer (string)"
43
+ )
44
  try:
45
+ result = call_llm(prompt)
46
+ questions = json.loads(result)
47
+
48
+ components = []
49
+ for idx, q in enumerate(questions):
50
+ q_md = gr.Markdown(f"**Q{idx+1}. {q['question']}**")
51
+ radio = gr.Radio(choices=q["options"], label="Choose an answer")
52
+ feedback = gr.Textbox(interactive=False)
53
 
54
+ def check(ans, correct=q["answer"]):
55
+ return "βœ… Correct!" if ans == correct else f"❌ Correct answer: {correct}"
 
 
56
 
57
+ radio.change(fn=check, inputs=[radio], outputs=[feedback])
58
+ components.extend([q_md, radio, feedback])
59
 
60
+ return gr.update(visible=True), components
 
61
 
62
+ except Exception as e:
63
+ return gr.update(visible=False), []
 
64
 
65
+ file_upload.change(fn=render_file, inputs=[file_upload], outputs=[file_viewer, file_text_state])
66
+ chat_input.submit(fn=ask_question, inputs=[chat_input, file_text_state], outputs=[chat_output])
67
+ generate_quiz_btn.click(fn=generate_quiz, inputs=[file_text_state], outputs=[quiz_container])
68
 
69
+ return file_viewer_dashboard