ankban commited on
Commit
72c5353
Β·
verified Β·
1 Parent(s): 3c5a443

Update file_viewer_panel.py

Browse files
Files changed (1) hide show
  1. file_viewer_panel.py +32 -17
file_viewer_panel.py CHANGED
@@ -5,24 +5,23 @@ from file_utils import extract_text_from_file, call_llm
5
 
6
  def file_viewer_dashboard(nickname_input):
7
  with gr.Column(scale=4, elem_id="main-column") as file_panel:
8
- with gr.Row():
9
- file_upload = gr.File(label="πŸ“‚ Upload a File", file_types=[".pdf", ".txt", ".md", ".pptx"])
10
-
11
  preview = gr.Textbox(label="πŸ“„ File Content Preview", lines=10, interactive=False)
 
12
  file_text_state = gr.State("")
 
13
 
14
- with gr.Row():
15
- chat_input = gr.Textbox(label="πŸ” Ask about the file")
16
- chat_output = gr.Chatbot()
17
 
18
- with gr.Row():
19
- btn_accessible = gr.Button("♿️ Improve Accessibility")
20
- accessible_box = gr.Textbox(label="🌍 Easier-to-Read Version", lines=15, interactive=False)
21
 
22
- with gr.Row():
23
- quiz_btn = gr.Button("🧠 Generate Quiz")
24
- quiz_score = gr.Textbox(label="Your Score", interactive=False, visible=False)
25
 
 
 
26
  quiz_box = gr.Column(visible=False)
27
 
28
  def load_and_preview(file):
@@ -31,9 +30,11 @@ def file_viewer_dashboard(nickname_input):
31
  text = extract_text_from_file(file)
32
  return text, text
33
 
34
- def handle_chat(query, text):
35
- prompt = f"Use this document to answer:\n\n{text}\n\nQuestion: {query}"
36
- return [(query, call_llm(prompt))]
 
 
37
 
38
  def make_accessible_version(text):
39
  prompt = f"""
@@ -44,6 +45,19 @@ Reformat the following content to improve readability and make it easier to foll
44
  - Use bullet points or headings when appropriate
45
  - Preserve the meaning clearly
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  Text:
48
  {text}
49
  """
@@ -72,8 +86,9 @@ Text:
72
  return gr.update(visible=False), [], gr.update(visible=True, value="Error generating quiz.")
73
 
74
  file_upload.change(fn=load_and_preview, inputs=[file_upload], outputs=[preview, file_text_state])
75
- chat_input.submit(fn=handle_chat, inputs=[chat_input, file_text_state], outputs=[chat_output])
76
  btn_accessible.click(fn=make_accessible_version, inputs=[file_text_state], outputs=[accessible_box])
 
77
  quiz_btn.click(fn=generate_quiz, inputs=[file_text_state], outputs=[quiz_box, quiz_box, quiz_score])
78
 
79
- return file_panel
 
5
 
6
  def file_viewer_dashboard(nickname_input):
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
+
11
  file_text_state = gr.State("")
12
+ chat_history = gr.State([])
13
 
14
+ chat_input = gr.Textbox(label="πŸ” Ask about the file")
15
+ chat_output = gr.Chatbot()
 
16
 
17
+ btn_accessible = gr.Button("♿️ Improve Accessibility")
18
+ accessible_box = gr.Textbox(label="🌍 Easier-to-Read Version", lines=15, interactive=False)
 
19
 
20
+ btn_readability = gr.Button("🧾 Improve Readability", size="sm")
21
+ readable_box = gr.Textbox(label="πŸ“˜ Reformatted Version", lines=15, interactive=False)
 
22
 
23
+ quiz_btn = gr.Button("🧠 Generate Quiz")
24
+ quiz_score = gr.Textbox(label="Your Score", interactive=False, visible=False)
25
  quiz_box = gr.Column(visible=False)
26
 
27
  def load_and_preview(file):
 
30
  text = extract_text_from_file(file)
31
  return text, text
32
 
33
+ def handle_chat(query, text, history):
34
+ prompt = f"Refer only to this file and answer:\n\n{text}\n\nQuestion: {query}"
35
+ answer = call_llm(prompt)
36
+ history.append((query, answer))
37
+ return history, ""
38
 
39
  def make_accessible_version(text):
40
  prompt = f"""
 
45
  - Use bullet points or headings when appropriate
46
  - Preserve the meaning clearly
47
 
48
+ Text:
49
+ {text}
50
+ """
51
+ return call_llm(prompt)
52
+
53
+ def improve_readability(text):
54
+ prompt = f"""
55
+ Please rewrite the following content to improve readability:
56
+ - Increase spacing
57
+ - Use larger font and clearer structure (for display purposes)
58
+ - Break into shorter paragraphs or bullet points
59
+ - Keep the meaning intact
60
+
61
  Text:
62
  {text}
63
  """
 
86
  return gr.update(visible=False), [], gr.update(visible=True, value="Error generating quiz.")
87
 
88
  file_upload.change(fn=load_and_preview, inputs=[file_upload], outputs=[preview, file_text_state])
89
+ chat_input.submit(fn=handle_chat, inputs=[chat_input, file_text_state, chat_history], outputs=[chat_output, chat_input])
90
  btn_accessible.click(fn=make_accessible_version, inputs=[file_text_state], outputs=[accessible_box])
91
+ btn_readability.click(fn=improve_readability, inputs=[file_text_state], outputs=[readable_box])
92
  quiz_btn.click(fn=generate_quiz, inputs=[file_text_state], outputs=[quiz_box, quiz_box, quiz_score])
93
 
94
+ return file_panel