ankban commited on
Commit
d3d2ca6
·
verified ·
1 Parent(s): c47a4d5

Update file_module.py

Browse files
Files changed (1) hide show
  1. file_module.py +21 -12
file_module.py CHANGED
@@ -12,6 +12,7 @@ def file_dashboard():
12
  uploaded_files = gr.State([])
13
  file_sections = gr.State({})
14
  file_texts = gr.State({})
 
15
 
16
  with gr.Row():
17
  # === LEFT SIDEBAR ===
@@ -40,8 +41,8 @@ ankban@wharton.upenn.edu""", elem_id="sidebar-user")
40
  file_chat_output = gr.Row(visible=False)
41
 
42
  with file_chat_output:
43
- file_preview = gr.Textbox(label="📄 File Viewer (Simulated)", lines=15, interactive=False)
44
- chat_output = gr.Textbox(label="💬 Chat Response", lines=8, interactive=False)
45
 
46
  chat_input = gr.Textbox(label="Ask something about this file")
47
 
@@ -69,17 +70,25 @@ ankban@wharton.upenn.edu""", elem_id="sidebar-user")
69
  return f"### {selected_filename} Study Guide\n\n{section_map[selected_filename]}"
70
 
71
  # === Show File Viewer + Chat ===
72
- def open_file_viewer():
73
- return gr.update(visible=True), "Simulated PDF viewer content...", ""
 
74
 
75
  # === GPT Chat Handler ===
76
- def chat_with_file(question, selected_file, text_map):
77
  if not selected_file or selected_file not in text_map:
78
- return "Please select a file first."
79
 
80
  text = text_map[selected_file]
81
- prompt = f"Answer the following question using only this document:\n\n{text}\n\nQuestion: {question}"
82
- return call_llm(prompt, system_message="You are a tutor helping a student understand their document.")
 
 
 
 
 
 
 
83
 
84
  file_upload.change(
85
  fn=store_file_and_generate_guide,
@@ -97,14 +106,14 @@ ankban@wharton.upenn.edu""", elem_id="sidebar-user")
97
 
98
  btn_file_view.click(
99
  fn=open_file_viewer,
100
- inputs=[],
101
  outputs=[file_chat_output, file_preview, chat_output]
102
  )
103
 
104
- chat_input.change(
105
  fn=chat_with_file,
106
- inputs=[chat_input, file_guide_list, file_texts],
107
- outputs=[chat_output],
108
  show_progress="minimal"
109
  )
110
 
 
12
  uploaded_files = gr.State([])
13
  file_sections = gr.State({})
14
  file_texts = gr.State({})
15
+ chat_history = gr.State({})
16
 
17
  with gr.Row():
18
  # === LEFT SIDEBAR ===
 
41
  file_chat_output = gr.Row(visible=False)
42
 
43
  with file_chat_output:
44
+ file_preview = gr.Textbox(label="📄 File Content Preview", lines=15, interactive=False)
45
+ chat_output = gr.Chatbot(label="💬 Chat with Document")
46
 
47
  chat_input = gr.Textbox(label="Ask something about this file")
48
 
 
70
  return f"### {selected_filename} Study Guide\n\n{section_map[selected_filename]}"
71
 
72
  # === Show File Viewer + Chat ===
73
+ def open_file_viewer(selected_file, text_map):
74
+ content = text_map.get(selected_file, "No file content available.")
75
+ return gr.update(visible=True), content, []
76
 
77
  # === GPT Chat Handler ===
78
+ def chat_with_file(question, selected_file, text_map, history_map):
79
  if not selected_file or selected_file not in text_map:
80
+ return [], history_map
81
 
82
  text = text_map[selected_file]
83
+ history = history_map.get(selected_file, [])
84
+
85
+ prompt = f"You are a helpful tutor. Answer this question using the document content below.\n\n{text}\n\nQuestion: {question}"
86
+ answer = call_llm(prompt)
87
+
88
+ new_history = history + [(question, answer)]
89
+ history_map[selected_file] = new_history
90
+
91
+ return new_history, history_map
92
 
93
  file_upload.change(
94
  fn=store_file_and_generate_guide,
 
106
 
107
  btn_file_view.click(
108
  fn=open_file_viewer,
109
+ inputs=[file_guide_list, file_texts],
110
  outputs=[file_chat_output, file_preview, chat_output]
111
  )
112
 
113
+ chat_input.submit(
114
  fn=chat_with_file,
115
+ inputs=[chat_input, file_guide_list, file_texts, chat_history],
116
+ outputs=[chat_output, chat_history],
117
  show_progress="minimal"
118
  )
119