Ali2206 commited on
Commit
4a6ed35
·
verified ·
1 Parent(s): 4f62914

Update ui/ui_core.py

Browse files
Files changed (1) hide show
  1. ui/ui_core.py +35 -5
ui/ui_core.py CHANGED
@@ -1,17 +1,48 @@
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  def create_ui(agent):
4
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
5
  gr.Markdown("<h1 style='text-align: center;'>💊 TxAgent: Therapeutic Reasoning</h1>")
6
  chatbot = gr.Chatbot(label="TxAgent", height=600, type="messages")
7
 
8
- file_upload = gr.File(label="Upload Medical File", file_types=[".pdf", ".txt", ".docx", ".jpg", ".png"])
9
  message_input = gr.Textbox(placeholder="Ask a biomedical question...", show_label=False)
10
  send_button = gr.Button("Send", variant="primary")
11
  conversation_state = gr.State([])
12
- file_state = gr.State(None)
13
 
14
- def handle_chat(message, history, conversation, uploaded_files):
 
 
 
 
 
 
 
 
15
  generator = agent.run_gradio_chat(
16
  message=message,
17
  history=history,
@@ -20,7 +51,6 @@ def create_ui(agent):
20
  max_token=8192,
21
  call_agent=False,
22
  conversation=conversation,
23
- uploaded_files=[uploaded_files] if uploaded_files else [],
24
  max_round=30
25
  )
26
  for update in generator:
@@ -37,4 +67,4 @@ def create_ui(agent):
37
  inputs=message_input,
38
  )
39
 
40
- return demo
 
1
  import gradio as gr
2
+ from PyPDF2 import PdfReader
3
+ import docx
4
+
5
+ def extract_text_from_uploaded_file(file_path):
6
+ if file_path.endswith(".pdf"):
7
+ try:
8
+ reader = PdfReader(file_path)
9
+ return "\n".join(page.extract_text() or "" for page in reader.pages)
10
+ except Exception as e:
11
+ return f"[Error extracting PDF text: {e}]"
12
+ elif file_path.endswith(".txt"):
13
+ try:
14
+ with open(file_path, "r", encoding="utf-8") as f:
15
+ return f.read()
16
+ except Exception as e:
17
+ return f"[Error reading text file: {e}]"
18
+ elif file_path.endswith(".docx"):
19
+ try:
20
+ doc = docx.Document(file_path)
21
+ return "\n".join([p.text for p in doc.paragraphs])
22
+ except Exception as e:
23
+ return f"[Error reading DOCX: {e}]"
24
+ else:
25
+ return "[Unsupported file type for text extraction]"
26
 
27
  def create_ui(agent):
28
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
29
  gr.Markdown("<h1 style='text-align: center;'>💊 TxAgent: Therapeutic Reasoning</h1>")
30
  chatbot = gr.Chatbot(label="TxAgent", height=600, type="messages")
31
 
32
+ file_upload = gr.File(label="Upload Medical File", file_types=[".pdf", ".txt", ".docx"])
33
  message_input = gr.Textbox(placeholder="Ask a biomedical question...", show_label=False)
34
  send_button = gr.Button("Send", variant="primary")
35
  conversation_state = gr.State([])
 
36
 
37
+ def handle_chat(message, history, conversation, uploaded_file):
38
+ file_text = ""
39
+ if uploaded_file:
40
+ file_text = extract_text_from_uploaded_file(uploaded_file.name)
41
+
42
+ # Append file text to the question
43
+ if file_text:
44
+ message += f"\n\n[Document Content Extracted from Upload]:\n{file_text}"
45
+
46
  generator = agent.run_gradio_chat(
47
  message=message,
48
  history=history,
 
51
  max_token=8192,
52
  call_agent=False,
53
  conversation=conversation,
 
54
  max_round=30
55
  )
56
  for update in generator:
 
67
  inputs=message_input,
68
  )
69
 
70
+ return demo