SamarthPujari commited on
Commit
3ebbd6b
·
verified ·
1 Parent(s): 37a7e5d

Update Gradio_UI.py

Browse files
Files changed (1) hide show
  1. Gradio_UI.py +29 -22
Gradio_UI.py CHANGED
@@ -1,9 +1,8 @@
1
  import gradio as gr
2
- import os # Import os to potentially handle file paths if needed, though Gradio provides the path
3
 
4
  class GradioUI:
5
  def __init__(self, agent):
6
- # The agent object passed from app.py
7
  self.agent = agent
8
 
9
  def process_input(self, query: str, pdf_file: gr.File = None):
@@ -33,6 +32,7 @@ class GradioUI:
33
  response = self.agent.tools[4](pdf_file.name, query)
34
  print("Document Q&A tool finished.")
35
  # Clean up the uploaded file after processing (optional but good practice)
 
36
  # if os.path.exists(pdf_file.name):
37
  # os.remove(pdf_file.name)
38
  return response
@@ -42,6 +42,7 @@ class GradioUI:
42
  except Exception as e:
43
  print(f"Error during Document Q&A tool execution: {e}")
44
  # Clean up the uploaded file on error too
 
45
  # if os.path.exists(pdf_file.name):
46
  # os.remove(pdf_file.name)
47
  return f"An error occurred during Document Q&A: {str(e)}"
@@ -61,6 +62,7 @@ class GradioUI:
61
  # Handle cases where only a PDF is uploaded without a question, or no input at all
62
  elif pdf_file is not None:
63
  # Clean up the uploaded file if no question was provided
 
64
  # if os.path.exists(pdf_file.name):
65
  # os.remove(pdf_file.name)
66
  return "Please enter a question in the textbox to ask about the uploaded PDF."
@@ -70,9 +72,8 @@ class GradioUI:
70
 
71
  def launch(self):
72
  """
73
- Launches the Gradio user interface for the agent with file upload capability.
74
  """
75
- # Use gr.Blocks for a more flexible layout
76
  with gr.Blocks() as demo:
77
  gr.Markdown("# Multi-Tool AI Agent with Document Upload")
78
  gr.Markdown(
@@ -81,33 +82,40 @@ class GradioUI:
81
  "Otherwise, use the text box for general requests (weather, search, etc.)."
82
  )
83
 
84
- # Text input for the user's query or question
85
- query_input = gr.Textbox(
86
- label="Enter your request or question:",
87
- placeholder="e.g., What is the weather in London? Search for the latest news about AI. What does this document say about [topic]? Summarize the document.",
88
- lines=3, # Provides more space for typing longer queries
89
- interactive=True # User can type in this box
90
- )
91
-
92
- # File upload component specifically for PDF documents
93
- pdf_upload = gr.File(
94
- label="Upload a PDF Document (Optional - for Document Q&A)",
95
- file_types=[".pdf"], # Restrict file types to PDF
96
- interactive=True # User can upload files
97
- )
 
 
 
 
 
 
 
98
 
99
  # Single output field to display the agent's final response
100
  agent_output = gr.Textbox(
101
  label="Agent's Response:",
102
  interactive=False, # User cannot type in the output box
103
  lines=10, # Provides more space to display potentially long responses
104
- autoscroll=True # Automatically scrolls to the latest output
105
  )
106
 
107
  # Button to trigger the processing function
108
  submit_btn = gr.Button("Submit")
109
 
110
- # Link the button click event to the process_input function
111
  # Pass both query_input (text) and pdf_upload (file) as inputs
112
  submit_btn.click(
113
  fn=self.process_input,
@@ -116,7 +124,6 @@ class GradioUI:
116
  )
117
 
118
  # Optional examples - adjust these based on your tools and whether you have default files
119
- # You would need to add example queries and potentially example files if you use them.
120
  # examples = [
121
  # ["What is the time in Berlin?"],
122
  # ["Generate an image of a robot cooking pasta."],
@@ -126,7 +133,7 @@ class GradioUI:
126
  # ]
127
  # gr.Examples(examples=examples, inputs=[query_input, pdf_upload]) # Examples take same inputs as function
128
 
129
-
130
  # Launch the Gradio interface
131
  # Setting share=True creates a public URL (useful for demos, be mindful of security/costs)
132
  # Setting inline=False opens the app in a new browser tab
 
1
  import gradio as gr
2
+ import os
3
 
4
  class GradioUI:
5
  def __init__(self, agent):
 
6
  self.agent = agent
7
 
8
  def process_input(self, query: str, pdf_file: gr.File = None):
 
32
  response = self.agent.tools[4](pdf_file.name, query)
33
  print("Document Q&A tool finished.")
34
  # Clean up the uploaded file after processing (optional but good practice)
35
+ # import os
36
  # if os.path.exists(pdf_file.name):
37
  # os.remove(pdf_file.name)
38
  return response
 
42
  except Exception as e:
43
  print(f"Error during Document Q&A tool execution: {e}")
44
  # Clean up the uploaded file on error too
45
+ # import os
46
  # if os.path.exists(pdf_file.name):
47
  # os.remove(pdf_file.name)
48
  return f"An error occurred during Document Q&A: {str(e)}"
 
62
  # Handle cases where only a PDF is uploaded without a question, or no input at all
63
  elif pdf_file is not None:
64
  # Clean up the uploaded file if no question was provided
65
+ # import os
66
  # if os.path.exists(pdf_file.name):
67
  # os.remove(pdf_file.name)
68
  return "Please enter a question in the textbox to ask about the uploaded PDF."
 
72
 
73
  def launch(self):
74
  """
75
+ Launches the Gradio user interface with layout adjustments.
76
  """
 
77
  with gr.Blocks() as demo:
78
  gr.Markdown("# Multi-Tool AI Agent with Document Upload")
79
  gr.Markdown(
 
82
  "Otherwise, use the text box for general requests (weather, search, etc.)."
83
  )
84
 
85
+ # Use a gr.Row to place input components side-by-side (or stacked on small screens)
86
+ with gr.Row():
87
+ # Text input for the user's query or question
88
+ # Give it more space (e.g., scale=3) compared to the file upload
89
+ query_input = gr.Textbox(
90
+ label="Enter your request or question:",
91
+ placeholder="e.g., What is the weather in London? Search for the latest news about AI. What does this document say about [topic]? Summarize the document.",
92
+ lines=3,
93
+ interactive=True,
94
+ scale=3 # This component will take 3 parts of the available width in the row
95
+ )
96
+
97
+ # File upload component for PDF documents
98
+ # Give it less space (e.g., scale=1 or scale=0)
99
+ pdf_upload = gr.File(
100
+ label="Upload PDF (Optional)", # Can also shorten the label
101
+ file_types=[".pdf"], # Restrict file types to PDF
102
+ interactive=True, # User can upload files
103
+ scale=1 # This component will take 1 part of the available width in the row
104
+ # Using scale=0 would make it take the minimum possible space
105
+ )
106
 
107
  # Single output field to display the agent's final response
108
  agent_output = gr.Textbox(
109
  label="Agent's Response:",
110
  interactive=False, # User cannot type in the output box
111
  lines=10, # Provides more space to display potentially long responses
112
+ autoscroll=True
113
  )
114
 
115
  # Button to trigger the processing function
116
  submit_btn = gr.Button("Submit")
117
 
118
+ # Link the button click to the process_input function
119
  # Pass both query_input (text) and pdf_upload (file) as inputs
120
  submit_btn.click(
121
  fn=self.process_input,
 
124
  )
125
 
126
  # Optional examples - adjust these based on your tools and whether you have default files
 
127
  # examples = [
128
  # ["What is the time in Berlin?"],
129
  # ["Generate an image of a robot cooking pasta."],
 
133
  # ]
134
  # gr.Examples(examples=examples, inputs=[query_input, pdf_upload]) # Examples take same inputs as function
135
 
136
+
137
  # Launch the Gradio interface
138
  # Setting share=True creates a public URL (useful for demos, be mindful of security/costs)
139
  # Setting inline=False opens the app in a new browser tab