on1onmangoes commited on
Commit
81c9675
·
verified ·
1 Parent(s): 0d9856e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +329 -136
app.py CHANGED
@@ -3,62 +3,47 @@ from gradio_client import Client, handle_file
3
  import os
4
 
5
  # Define your Hugging Face token (make sure to set it as an environment variable)
6
- HF_TOKEN = os.getenv("HF_TOKEN") # Replace with your actual token if not using env variable
7
 
8
  # Initialize the Gradio Client for the specified API
9
  client = Client("on1onmangoes/CNIHUB10724v9", hf_token=HF_TOKEN)
10
 
11
  # Authentication function
12
  def login(username, password):
13
- if username == "your_username" and password == "your_password": # Update with actual credentials
14
- return True
15
- else:
16
- return False
17
-
18
- # Function to handle different API calls based on user input
19
- def handle_api_call(username, password, message=None, client_name="rosariarossi",
20
- system_prompt="You are an expert assistant", num_retrieved_docs=10,
21
- num_docs_final=9, temperature=0, max_new_tokens=1024,
22
- top_p=1, top_k=20, penalty=1.2,
23
- pdf_file=None, query=None, question=None):
24
-
25
- if not login(username, password):
26
- return "Invalid credentials! Please try again."
27
-
28
- if message:
29
- # Handle chat message
30
- chat_result = client.predict(
31
- message=message,
32
- client_name=client_name,
33
- system_prompt=system_prompt,
34
- num_retrieved_docs=num_retrieved_docs,
35
- num_docs_final=num_docs_final,
36
- temperature=temperature,
37
- max_new_tokens=max_new_tokens,
38
- top_p=top_p,
39
- top_k=top_k,
40
- penalty=penalty,
41
- api_name="/chat"
42
- )
43
- return chat_result
44
- elif pdf_file:
45
- # Handle PDF file
46
- pdf_result = client.predict(
47
- pdf_file=handle_file(pdf_file),
48
- client_name=client_name,
49
- api_name="/process_pdf2"
50
- )
51
- return pdf_result[1] # Returning the string result from the PDF processing
52
- elif query:
53
- # Handle search query
54
- search_result = client.predict(query=query, api_name="/search_with_confidence")
55
- return search_result
56
- elif question:
57
- # Handle question for RAG
58
- rag_result = client.predict(question=question, api_name="/answer_with_rag")
59
- return rag_result
60
- else:
61
- return "No valid input provided!"
62
 
63
  # Create the Gradio Blocks interface
64
  with gr.Blocks() as app:
@@ -69,109 +54,130 @@ with gr.Blocks() as app:
69
  password_input = gr.Textbox(label="Password", placeholder="Enter your password", type="password")
70
 
71
  with gr.Tab("Chat"):
72
- message_input = gr.Textbox(label="Message", placeholder="Type your message here")
73
-
74
- gr.Markdown("### Client Options")
75
- client_name_dropdown = gr.Dropdown(
76
- label="Select Client",
77
- choices=["rosariarossi", "bianchifiordaliso", "lorenzoverdi"],
78
- value="rosariarossi"
79
- )
80
-
81
- system_prompt_input = gr.Textbox(
82
- label="System Prompt",
83
- placeholder="Enter system prompt here",
84
- value="You are an expert assistant"
85
- )
86
-
87
- num_retrieved_docs_slider = gr.Slider(
88
- label="Number of Initial Documents to Retrieve",
89
- minimum=1,
90
- maximum=100,
91
- step=1,
92
- value=10
93
- )
94
 
95
- num_docs_final_slider = gr.Slider(
96
- label="Number of Final Documents to Retrieve",
97
- minimum=1,
98
- maximum=100,
99
- step=1,
100
- value=9
101
- )
102
 
103
- temperature_slider = gr.Slider(
104
- label="Temperature",
105
- minimum=0,
106
- maximum=2,
107
- step=0.1,
108
- value=0
109
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
- max_new_tokens_slider = gr.Slider(
112
- label="Max New Tokens",
113
- minimum=1,
114
- maximum=2048,
115
- step=1,
116
- value=1024
 
 
 
 
 
 
 
 
 
 
117
  )
118
-
119
- top_p_slider = gr.Slider(
120
- label="Top P",
121
- minimum=0,
122
- maximum=1,
123
- step=0.01,
124
- value=1
125
- )
126
-
127
- top_k_slider = gr.Slider(
128
- label="Top K",
129
- minimum=1,
130
- maximum=100,
131
- step=1,
132
- value=20
133
- )
134
-
135
- penalty_slider = gr.Slider(
136
- label="Repetition Penalty",
137
- minimum=1,
138
- maximum=5,
139
- step=0.1,
140
- value=1.2
141
- )
142
-
143
- chat_output = gr.Textbox(label="Chat Response", interactive=False)
144
 
145
  with gr.Tab("Process PDF"):
146
  pdf_input = gr.File(label="Upload PDF File")
147
  pdf_output = gr.Textbox(label="PDF Result", interactive=False)
148
 
 
 
 
 
 
 
 
149
  with gr.Tab("Search"):
150
  query_input = gr.Textbox(label="Enter Search Query")
151
  search_output = gr.Textbox(label="Search Confidence Result", interactive=False)
152
 
 
 
 
 
 
 
 
153
  with gr.Tab("Answer with RAG"):
154
  question_input = gr.Textbox(label="Enter Question for RAG")
155
  rag_output = gr.Textbox(label="RAG Answer Result", interactive=False)
156
 
157
- api_button = gr.Button("Submit")
158
-
159
- # Bind the button click to the handle_api_call function
160
- api_button.click(
161
- handle_api_call,
162
- inputs=[
163
- username_input, password_input,
164
- message_input, client_name_dropdown,
165
- system_prompt_input, num_retrieved_docs_slider,
166
- num_docs_final_slider, temperature_slider,
167
- max_new_tokens_slider, top_p_slider,
168
- top_k_slider, penalty_slider,
169
- pdf_input, query_input, question_input
170
- ],
171
- outputs=[
172
- chat_output, pdf_output, search_output, rag_output
173
- ]
174
- )
175
 
176
  # Launch the app
177
  app.launch()
@@ -182,6 +188,193 @@ app.launch()
182
 
183
 
184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  # import gradio as gr
186
  # from gradio_client import Client, handle_file
187
  # import os
 
3
  import os
4
 
5
  # Define your Hugging Face token (make sure to set it as an environment variable)
6
+ HF_TOKEN = os.getenv("HF_TOKEN") # Replace with your actual token if not using an environment variable
7
 
8
  # Initialize the Gradio Client for the specified API
9
  client = Client("on1onmangoes/CNIHUB10724v9", hf_token=HF_TOKEN)
10
 
11
  # Authentication function
12
  def login(username, password):
13
+ return username == "your_username" and password == "your_password" # Update with actual credentials
14
+
15
+ # Function to handle chat API call
16
+ def stream_chat_with_rag(message, client_name, system_prompt, num_retrieved_docs, num_docs_final, temperature, max_new_tokens, top_p, top_k, penalty):
17
+ response = client.predict(
18
+ message=message,
19
+ client_name=client_name,
20
+ system_prompt=system_prompt,
21
+ num_retrieved_docs=num_retrieved_docs,
22
+ num_docs_final=num_docs_final,
23
+ temperature=temperature,
24
+ max_new_tokens=max_new_tokens,
25
+ top_p=top_p,
26
+ top_k=top_k,
27
+ penalty=penalty,
28
+ api_name="/chat"
29
+ )
30
+ return response
31
+
32
+ # Function to handle PDF processing API call
33
+ def process_pdf(pdf_file, client_name):
34
+ return client.predict(
35
+ pdf_file=handle_file(pdf_file),
36
+ client_name=client_name,
37
+ api_name="/process_pdf2"
38
+ )[1] # Return only the result string
39
+
40
+ # Function to handle search API call
41
+ def search_api(query):
42
+ return client.predict(query=query, api_name="/search_with_confidence")
43
+
44
+ # Function to handle RAG API call
45
+ def rag_api(question):
46
+ return client.predict(question=question, api_name="/answer_with_rag")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  # Create the Gradio Blocks interface
49
  with gr.Blocks() as app:
 
54
  password_input = gr.Textbox(label="Password", placeholder="Enter your password", type="password")
55
 
56
  with gr.Tab("Chat"):
57
+ chatbot = gr.Chatbot(fill_height=True) # Create a chatbot interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
+ # Create an accordion for additional input parameters
60
+ additional_inputs_accordion = gr.Accordion(label="⚙️ Parameters", open=False, render=False)
 
 
 
 
 
61
 
62
+ with additional_inputs_accordion:
63
+ client_name_dropdown = gr.Dropdown(
64
+ label="Select Client",
65
+ choices=["rosariarossi", "bianchifiordaliso", "lorenzoverdi"],
66
+ value="rosariarossi",
67
+ render=False
68
+ )
69
+ system_prompt_input = gr.Textbox(
70
+ value="You are an expert assistant",
71
+ label="System Prompt",
72
+ render=False
73
+ )
74
+ num_retrieved_docs_slider = gr.Slider(
75
+ label="Number of Initial Documents to Retrieve",
76
+ minimum=1,
77
+ maximum=10,
78
+ step=1,
79
+ value=10,
80
+ render=False
81
+ )
82
+ num_docs_final_slider = gr.Slider(
83
+ label="Number of Final Documents to Retrieve",
84
+ minimum=1,
85
+ maximum=10,
86
+ step=1,
87
+ value=9,
88
+ render=False
89
+ )
90
+ temperature_slider = gr.Slider(
91
+ label="Temperature",
92
+ minimum=0.2,
93
+ maximum=1,
94
+ step=0.1,
95
+ value=0,
96
+ render=False
97
+ )
98
+ max_new_tokens_slider = gr.Slider(
99
+ label="Max New Tokens",
100
+ minimum=128,
101
+ maximum=8192,
102
+ step=1,
103
+ value=1024,
104
+ render=False
105
+ )
106
+ top_p_slider = gr.Slider(
107
+ label="Top P",
108
+ minimum=0.0,
109
+ maximum=1.0,
110
+ step=0.1,
111
+ value=1.0,
112
+ render=False
113
+ )
114
+ top_k_slider = gr.Slider(
115
+ label="Top K",
116
+ minimum=1,
117
+ maximum=20,
118
+ step=1,
119
+ value=20,
120
+ render=False
121
+ )
122
+ penalty_slider = gr.Slider(
123
+ label="Repetition Penalty",
124
+ minimum=0.0,
125
+ maximum=2.0,
126
+ step=0.1,
127
+ value=1.2,
128
+ render=False
129
+ )
130
 
131
+ chat_interface = gr.ChatInterface(
132
+ fn=stream_chat_with_rag,
133
+ chatbot=chatbot,
134
+ fill_height=True,
135
+ additional_inputs_accordion=additional_inputs_accordion,
136
+ additional_inputs=[
137
+ client_name_dropdown,
138
+ system_prompt_input,
139
+ num_retrieved_docs_slider,
140
+ num_docs_final_slider,
141
+ temperature_slider,
142
+ max_new_tokens_slider,
143
+ top_p_slider,
144
+ top_k_slider,
145
+ penalty_slider,
146
+ ],
147
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
 
149
  with gr.Tab("Process PDF"):
150
  pdf_input = gr.File(label="Upload PDF File")
151
  pdf_output = gr.Textbox(label="PDF Result", interactive=False)
152
 
153
+ pdf_button = gr.Button("Process PDF")
154
+ pdf_button.click(
155
+ process_pdf,
156
+ inputs=[pdf_input, client_name_dropdown],
157
+ outputs=pdf_output
158
+ )
159
+
160
  with gr.Tab("Search"):
161
  query_input = gr.Textbox(label="Enter Search Query")
162
  search_output = gr.Textbox(label="Search Confidence Result", interactive=False)
163
 
164
+ search_button = gr.Button("Search")
165
+ search_button.click(
166
+ search_api,
167
+ inputs=query_input,
168
+ outputs=search_output
169
+ )
170
+
171
  with gr.Tab("Answer with RAG"):
172
  question_input = gr.Textbox(label="Enter Question for RAG")
173
  rag_output = gr.Textbox(label="RAG Answer Result", interactive=False)
174
 
175
+ rag_button = gr.Button("Get Answer")
176
+ rag_button.click(
177
+ rag_api,
178
+ inputs=question_input,
179
+ outputs=rag_output
180
+ )
 
 
 
 
 
 
 
 
 
 
 
 
181
 
182
  # Launch the app
183
  app.launch()
 
188
 
189
 
190
 
191
+
192
+
193
+
194
+ # import gradio as gr
195
+ # from gradio_client import Client, handle_file
196
+ # import os
197
+
198
+ # # Define your Hugging Face token (make sure to set it as an environment variable)
199
+ # HF_TOKEN = os.getenv("HF_TOKEN") # Replace with your actual token if not using env variable
200
+
201
+ # # Initialize the Gradio Client for the specified API
202
+ # client = Client("on1onmangoes/CNIHUB10724v9", hf_token=HF_TOKEN)
203
+
204
+ # # Authentication function
205
+ # def login(username, password):
206
+ # if username == "your_username" and password == "your_password": # Update with actual credentials
207
+ # return True
208
+ # else:
209
+ # return False
210
+
211
+ # # Function to handle different API calls based on user input
212
+ # def handle_api_call(username, password, message=None, client_name="rosariarossi",
213
+ # system_prompt="You are an expert assistant", num_retrieved_docs=10,
214
+ # num_docs_final=9, temperature=0, max_new_tokens=1024,
215
+ # top_p=1, top_k=20, penalty=1.2,
216
+ # pdf_file=None, query=None, question=None):
217
+
218
+ # if not login(username, password):
219
+ # return "Invalid credentials! Please try again."
220
+
221
+ # if message:
222
+ # # Handle chat message
223
+ # chat_result = client.predict(
224
+ # message=message,
225
+ # client_name=client_name,
226
+ # system_prompt=system_prompt,
227
+ # num_retrieved_docs=num_retrieved_docs,
228
+ # num_docs_final=num_docs_final,
229
+ # temperature=temperature,
230
+ # max_new_tokens=max_new_tokens,
231
+ # top_p=top_p,
232
+ # top_k=top_k,
233
+ # penalty=penalty,
234
+ # api_name="/chat"
235
+ # )
236
+ # return chat_result
237
+ # elif pdf_file:
238
+ # # Handle PDF file
239
+ # pdf_result = client.predict(
240
+ # pdf_file=handle_file(pdf_file),
241
+ # client_name=client_name,
242
+ # api_name="/process_pdf2"
243
+ # )
244
+ # return pdf_result[1] # Returning the string result from the PDF processing
245
+ # elif query:
246
+ # # Handle search query
247
+ # search_result = client.predict(query=query, api_name="/search_with_confidence")
248
+ # return search_result
249
+ # elif question:
250
+ # # Handle question for RAG
251
+ # rag_result = client.predict(question=question, api_name="/answer_with_rag")
252
+ # return rag_result
253
+ # else:
254
+ # return "No valid input provided!"
255
+
256
+ # # Create the Gradio Blocks interface
257
+ # with gr.Blocks() as app:
258
+ # gr.Markdown("### Login")
259
+
260
+ # with gr.Row():
261
+ # username_input = gr.Textbox(label="Username", placeholder="Enter your username")
262
+ # password_input = gr.Textbox(label="Password", placeholder="Enter your password", type="password")
263
+
264
+ # with gr.Tab("Chat"):
265
+ # message_input = gr.Textbox(label="Message", placeholder="Type your message here")
266
+
267
+ # gr.Markdown("### Client Options")
268
+ # client_name_dropdown = gr.Dropdown(
269
+ # label="Select Client",
270
+ # choices=["rosariarossi", "bianchifiordaliso", "lorenzoverdi"],
271
+ # value="rosariarossi"
272
+ # )
273
+
274
+ # system_prompt_input = gr.Textbox(
275
+ # label="System Prompt",
276
+ # placeholder="Enter system prompt here",
277
+ # value="You are an expert assistant"
278
+ # )
279
+
280
+ # num_retrieved_docs_slider = gr.Slider(
281
+ # label="Number of Initial Documents to Retrieve",
282
+ # minimum=1,
283
+ # maximum=100,
284
+ # step=1,
285
+ # value=10
286
+ # )
287
+
288
+ # num_docs_final_slider = gr.Slider(
289
+ # label="Number of Final Documents to Retrieve",
290
+ # minimum=1,
291
+ # maximum=100,
292
+ # step=1,
293
+ # value=9
294
+ # )
295
+
296
+ # temperature_slider = gr.Slider(
297
+ # label="Temperature",
298
+ # minimum=0,
299
+ # maximum=2,
300
+ # step=0.1,
301
+ # value=0
302
+ # )
303
+
304
+ # max_new_tokens_slider = gr.Slider(
305
+ # label="Max New Tokens",
306
+ # minimum=1,
307
+ # maximum=2048,
308
+ # step=1,
309
+ # value=1024
310
+ # )
311
+
312
+ # top_p_slider = gr.Slider(
313
+ # label="Top P",
314
+ # minimum=0,
315
+ # maximum=1,
316
+ # step=0.01,
317
+ # value=1
318
+ # )
319
+
320
+ # top_k_slider = gr.Slider(
321
+ # label="Top K",
322
+ # minimum=1,
323
+ # maximum=100,
324
+ # step=1,
325
+ # value=20
326
+ # )
327
+
328
+ # penalty_slider = gr.Slider(
329
+ # label="Repetition Penalty",
330
+ # minimum=1,
331
+ # maximum=5,
332
+ # step=0.1,
333
+ # value=1.2
334
+ # )
335
+
336
+ # chat_output = gr.Textbox(label="Chat Response", interactive=False)
337
+
338
+ # with gr.Tab("Process PDF"):
339
+ # pdf_input = gr.File(label="Upload PDF File")
340
+ # pdf_output = gr.Textbox(label="PDF Result", interactive=False)
341
+
342
+ # with gr.Tab("Search"):
343
+ # query_input = gr.Textbox(label="Enter Search Query")
344
+ # search_output = gr.Textbox(label="Search Confidence Result", interactive=False)
345
+
346
+ # with gr.Tab("Answer with RAG"):
347
+ # question_input = gr.Textbox(label="Enter Question for RAG")
348
+ # rag_output = gr.Textbox(label="RAG Answer Result", interactive=False)
349
+
350
+ # api_button = gr.Button("Submit")
351
+
352
+ # # Bind the button click to the handle_api_call function
353
+ # api_button.click(
354
+ # handle_api_call,
355
+ # inputs=[
356
+ # username_input, password_input,
357
+ # message_input, client_name_dropdown,
358
+ # system_prompt_input, num_retrieved_docs_slider,
359
+ # num_docs_final_slider, temperature_slider,
360
+ # max_new_tokens_slider, top_p_slider,
361
+ # top_k_slider, penalty_slider,
362
+ # pdf_input, query_input, question_input
363
+ # ],
364
+ # outputs=[
365
+ # chat_output, pdf_output, search_output, rag_output
366
+ # ]
367
+ # )
368
+
369
+ # # Launch the app
370
+ # app.launch()
371
+
372
+
373
+
374
+
375
+
376
+
377
+
378
  # import gradio as gr
379
  # from gradio_client import Client, handle_file
380
  # import os