ankban commited on
Commit
e580f8c
Β·
verified Β·
1 Parent(s): 131679d

Update file_viewer_panel.py

Browse files
Files changed (1) hide show
  1. file_viewer_panel.py +27 -14
file_viewer_panel.py CHANGED
@@ -1,45 +1,59 @@
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)
@@ -58,7 +72,6 @@ def file_viewer_dashboard():
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
 
@@ -66,4 +79,4 @@ def file_viewer_dashboard():
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
 
1
  import gradio as gr
2
+ import os
3
+ import shutil
4
+ import json
5
+ from pdf2image import convert_from_path
6
  from file_utils import extract_text_from_file, call_llm
 
7
 
8
  def file_viewer_dashboard():
9
  with gr.Row():
10
+ with gr.Column(scale=3):
11
  file_upload = gr.File(label="πŸ“‚ Upload a File", file_types=[".pdf", ".txt", ".md", ".pptx"])
12
  file_viewer = gr.HTML(label="πŸ“„ File Preview")
13
+
14
+ with gr.Column(scale=2):
15
  generate_quiz_btn = gr.Button("🧠 Practice Questions")
16
  quiz_container = gr.Column(visible=False)
17
  chat_output = gr.Chatbot(label="Ask a question to learn more about the file")
18
+ chat_input = gr.Textbox(label="", placeholder="Ask Chatter a question...")
19
 
20
  file_text_state = gr.State("")
21
 
22
  def render_file(file):
23
  if not file:
24
  return "", ""
25
+
26
  ext = os.path.splitext(file.name)[1].lower()
 
27
  text = extract_text_from_file(file)
28
 
29
  if ext == ".pdf":
30
+ # Convert to images
31
+ output_dir = "/tmp/pdf_images"
32
+ os.makedirs(output_dir, exist_ok=True)
33
+ images = convert_from_path(file.name, dpi=150, fmt="png", output_folder=output_dir)
34
+
35
+ img_tags = []
36
+ for i, img in enumerate(images):
37
+ img_path = os.path.join(output_dir, f"page_{i}.png")
38
+ img.save(img_path, "PNG")
39
+ img_tags.append(f"<img src='/file={img_path}' style='width:100%; margin-bottom: 10px; border-radius: 8px;'/>")
40
+
41
+ viewer_html = "".join(img_tags)
42
+
43
  else:
44
+ # Fallback for text/markdown/pptx
45
+ viewer_html = f"<pre style='padding:12px; background:#f9f9f9; white-space:pre-wrap;'>{text}</pre>"
46
 
47
  return viewer_html, text
48
 
49
  def ask_question(q, text):
50
+ prompt = f"Based on the document below, answer the user's question.\n\n{text}\n\nQuestion: {q}"
51
  return [(q, call_llm(prompt))]
52
 
53
  def generate_quiz(text):
54
  prompt = (
55
  "Generate 5 multiple choice questions from this document. "
56
+ "Return a JSON list where each item has:\n- question\n- options (list)\n- answer (correct one)"
57
  )
58
  try:
59
  result = call_llm(prompt)
 
72
  components.extend([q_md, radio, feedback])
73
 
74
  return gr.update(visible=True), components
 
75
  except Exception as e:
76
  return gr.update(visible=False), []
77
 
 
79
  chat_input.submit(fn=ask_question, inputs=[chat_input, file_text_state], outputs=[chat_output])
80
  generate_quiz_btn.click(fn=generate_quiz, inputs=[file_text_state], outputs=[quiz_container])
81
 
82
+ return file_panel