Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,7 @@ import os
|
|
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/
|
10 |
|
11 |
|
12 |
# Function to handle chat API call
|
@@ -40,58 +40,6 @@ def stream_chat_with_rag(
|
|
40 |
# Return the assistant's reply
|
41 |
return response
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
# # OG code in V9
|
47 |
-
# def stream_chat_with_rag(
|
48 |
-
# message: str,
|
49 |
-
# history: list,
|
50 |
-
# client_name: str,
|
51 |
-
# system_prompt: str,
|
52 |
-
# num_retrieved_docs: int = 10,
|
53 |
-
# num_docs_final: int = 9,
|
54 |
-
# temperature: float = 0,
|
55 |
-
# max_new_tokens: int = 1024,
|
56 |
-
# top_p: float = 1.0,
|
57 |
-
# top_k: int = 20,
|
58 |
-
# penalty: float = 1.2,
|
59 |
-
# ):
|
60 |
-
|
61 |
-
# # Function to handle chat API call
|
62 |
-
# # def stream_chat_with_rag(message, system_prompt, num_retrieved_docs, num_docs_final, temperature, max_new_tokens, top_p, top_k, penalty):
|
63 |
-
# # response = client.predict(
|
64 |
-
# # message=message,
|
65 |
-
# # client_name="rosariarossi", # Hardcoded client name
|
66 |
-
# # system_prompt=system_prompt,
|
67 |
-
# # num_retrieved_docs=num_retrieved_docs,
|
68 |
-
# # num_docs_final=num_docs_final,
|
69 |
-
# # temperature=temperature,
|
70 |
-
# # max_new_tokens=max_new_tokens,
|
71 |
-
# # top_p=top_p,
|
72 |
-
# # top_k=top_k,
|
73 |
-
# # penalty=penalty,
|
74 |
-
# # api_name="/chat"
|
75 |
-
# # )
|
76 |
-
# # return response
|
77 |
-
|
78 |
-
# result = client.predict(
|
79 |
-
# message=message,
|
80 |
-
# client_name="rosariarossi",
|
81 |
-
# system_prompt="You are an expert assistant",
|
82 |
-
# num_retrieved_docs=10,
|
83 |
-
# num_docs_final=9,
|
84 |
-
# temperature=0,
|
85 |
-
# max_new_tokens=1024,
|
86 |
-
# top_p=1,
|
87 |
-
# top_k=20,
|
88 |
-
# penalty=1.2,
|
89 |
-
# api_name="/chat"
|
90 |
-
# )
|
91 |
-
# return result
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
# Function to handle PDF processing API call
|
96 |
def process_pdf(pdf_file):
|
97 |
return client.predict(
|
@@ -121,84 +69,6 @@ TITLE = "<h1 style='text-align:center;'>My Gradio Chat App</h1>"
|
|
121 |
# Create the Gradio Blocks interface
|
122 |
with gr.Blocks(css=CSS) as demo:
|
123 |
gr.HTML(TITLE)
|
124 |
-
## OG v9 comments
|
125 |
-
# gr.ChatInterface(
|
126 |
-
# fn=stream_chat_with_rag,
|
127 |
-
# chatbot=chatbot,
|
128 |
-
# fill_height=True,
|
129 |
-
# #gr.dropdown(['rosariarossi','bianchifiordaliso','lorenzoverdi'],label="Select Client"),
|
130 |
-
# additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
|
131 |
-
# additional_inputs=[
|
132 |
-
# gr.Dropdown(['rosariarossi','bianchifiordaliso','lorenzoverdi'],value="rosariarossi",label="Select Client", render=False,),
|
133 |
-
# gr.Textbox(
|
134 |
-
# # value="""Using the information contained in the context,
|
135 |
-
# # give a comprehensive answer to the question.
|
136 |
-
# # Respond only to the question asked, response should be concise and relevant to the question.
|
137 |
-
# # Provide the number of the source document when relevant.
|
138 |
-
# # If the answer cannot be deduced from the context, do not give an answer""",
|
139 |
-
# value ="""You are an expert assistant""",
|
140 |
-
# label="System Prompt",
|
141 |
-
# render=False,
|
142 |
-
# ),
|
143 |
-
# gr.Slider(
|
144 |
-
# minimum=1,
|
145 |
-
# maximum=10,
|
146 |
-
# step=1,
|
147 |
-
# value=10,
|
148 |
-
# label="Number of Initial Documents to Retrieve",
|
149 |
-
# render=False,
|
150 |
-
# ),
|
151 |
-
# gr.Slider(
|
152 |
-
# minimum=1,
|
153 |
-
# maximum=10,
|
154 |
-
# step=1,
|
155 |
-
# value=9,
|
156 |
-
# label="Number of Final Documents to Retrieve",
|
157 |
-
# render=False,
|
158 |
-
# ),
|
159 |
-
# gr.Slider(
|
160 |
-
# minimum=0.2,
|
161 |
-
# maximum=1,
|
162 |
-
# step=0.1,
|
163 |
-
# value=0,
|
164 |
-
# label="Temperature",
|
165 |
-
# render=False,
|
166 |
-
# ),
|
167 |
-
# gr.Slider(
|
168 |
-
# minimum=128,
|
169 |
-
# maximum=8192,
|
170 |
-
# step=1,
|
171 |
-
# value=1024,
|
172 |
-
# label="Max new tokens",
|
173 |
-
# render=False,
|
174 |
-
# ),
|
175 |
-
# gr.Slider(
|
176 |
-
# minimum=0.0,
|
177 |
-
# maximum=1.0,
|
178 |
-
# step=0.1,
|
179 |
-
# value=1.0,
|
180 |
-
# label="top_p",
|
181 |
-
# render=False,
|
182 |
-
# ),
|
183 |
-
# gr.Slider(
|
184 |
-
# minimum=1,
|
185 |
-
# maximum=20,
|
186 |
-
# step=1,
|
187 |
-
# value=20,
|
188 |
-
# label="top_k",
|
189 |
-
# render=False,
|
190 |
-
# ),
|
191 |
-
# gr.Slider(
|
192 |
-
# minimum=0.0,
|
193 |
-
# maximum=2.0,
|
194 |
-
# step=0.1,
|
195 |
-
# value=1.2,
|
196 |
-
# label="Repetition penalty",
|
197 |
-
# render=False,
|
198 |
-
# ),
|
199 |
-
# ],
|
200 |
-
|
201 |
-
# )
|
202 |
with gr.Tab("Chat"):
|
203 |
chatbot = gr.Chatbot() # Create a chatbot interface
|
204 |
|
@@ -310,681 +180,3 @@ if __name__ == "__main__":
|
|
310 |
demo.launch()
|
311 |
|
312 |
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
# import gradio as gr
|
321 |
-
# from gradio_client import Client, handle_file
|
322 |
-
# import os
|
323 |
-
|
324 |
-
# # Define your Hugging Face token (make sure to set it as an environment variable)
|
325 |
-
# HF_TOKEN = os.getenv("HF_TOKEN") # Replace with your actual token if not using an environment variable
|
326 |
-
|
327 |
-
# # Initialize the Gradio Client for the specified API
|
328 |
-
# client = Client("on1onmangoes/CNIHUB10724v9", hf_token=HF_TOKEN)
|
329 |
-
|
330 |
-
# # Function to handle chat API call
|
331 |
-
# def stream_chat_with_rag(message, system_prompt, num_retrieved_docs, num_docs_final, temperature, max_new_tokens, top_p, top_k, penalty):
|
332 |
-
# response = client.predict(
|
333 |
-
# message=message,
|
334 |
-
# client_name="rosariarossi", # Hardcoded client name
|
335 |
-
# system_prompt=system_prompt,
|
336 |
-
# num_retrieved_docs=num_retrieved_docs,
|
337 |
-
# num_docs_final=num_docs_final,
|
338 |
-
# temperature=temperature,
|
339 |
-
# max_new_tokens=max_new_tokens,
|
340 |
-
# top_p=top_p,
|
341 |
-
# top_k=top_k,
|
342 |
-
# penalty=penalty,
|
343 |
-
# api_name="/chat"
|
344 |
-
# )
|
345 |
-
# return response
|
346 |
-
|
347 |
-
# # Function to handle PDF processing API call
|
348 |
-
# def process_pdf(pdf_file):
|
349 |
-
# return client.predict(
|
350 |
-
# pdf_file=handle_file(pdf_file),
|
351 |
-
# client_name="rosariarossi", # Hardcoded client name
|
352 |
-
# api_name="/process_pdf2"
|
353 |
-
# )[1] # Return only the result string
|
354 |
-
|
355 |
-
# # Function to handle search API call
|
356 |
-
# def search_api(query):
|
357 |
-
# return client.predict(query=query, api_name="/search_with_confidence")
|
358 |
-
|
359 |
-
# # Function to handle RAG API call
|
360 |
-
# def rag_api(question):
|
361 |
-
# return client.predict(question=question, api_name="/answer_with_rag")
|
362 |
-
|
363 |
-
# # CSS for custom styling
|
364 |
-
# CSS = """
|
365 |
-
# # chat-container {
|
366 |
-
# height: 100vh;
|
367 |
-
# }
|
368 |
-
# """
|
369 |
-
|
370 |
-
# # Title for the application
|
371 |
-
# TITLE = "<h1 style='text-align:center;'>My Gradio Chat App</h1>"
|
372 |
-
|
373 |
-
# # Create the Gradio Blocks interface
|
374 |
-
# with gr.Blocks(css=CSS) as demo:
|
375 |
-
# gr.HTML(TITLE)
|
376 |
-
|
377 |
-
# with gr.Tab("Chat"):
|
378 |
-
# chatbot = gr.Chatbot() # Create a chatbot interface
|
379 |
-
|
380 |
-
# chat_interface = gr.ChatInterface(
|
381 |
-
# fn=stream_chat_with_rag,
|
382 |
-
# chatbot=chatbot,
|
383 |
-
# additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
|
384 |
-
# additional_inputs=[
|
385 |
-
# gr.Dropdown(
|
386 |
-
# ['rosariarossi', 'bianchifiordaliso', 'lorenzoverdi'],
|
387 |
-
# value="rosariarossi",
|
388 |
-
# label="Select Client",
|
389 |
-
# render=False,
|
390 |
-
# ),
|
391 |
-
# gr.Textbox(
|
392 |
-
# value="You are an expert assistant",
|
393 |
-
# label="System Prompt",
|
394 |
-
# render=False,
|
395 |
-
# ),
|
396 |
-
# gr.Slider(
|
397 |
-
# minimum=1,
|
398 |
-
# maximum=10,
|
399 |
-
# step=1,
|
400 |
-
# value=10,
|
401 |
-
# label="Number of Initial Documents to Retrieve",
|
402 |
-
# render=False,
|
403 |
-
# ),
|
404 |
-
# gr.Slider(
|
405 |
-
# minimum=1,
|
406 |
-
# maximum=10,
|
407 |
-
# step=1,
|
408 |
-
# value=9,
|
409 |
-
# label="Number of Final Documents to Retrieve",
|
410 |
-
# render=False,
|
411 |
-
# ),
|
412 |
-
# gr.Slider(
|
413 |
-
# minimum=0.2,
|
414 |
-
# maximum=1,
|
415 |
-
# step=0.1,
|
416 |
-
# value=0,
|
417 |
-
# label="Temperature",
|
418 |
-
# render=False,
|
419 |
-
# ),
|
420 |
-
# gr.Slider(
|
421 |
-
# minimum=128,
|
422 |
-
# maximum=8192,
|
423 |
-
# step=1,
|
424 |
-
# value=1024,
|
425 |
-
# label="Max new tokens",
|
426 |
-
# render=False,
|
427 |
-
# ),
|
428 |
-
# gr.Slider(
|
429 |
-
# minimum=0.0,
|
430 |
-
# maximum=1.0,
|
431 |
-
# step=0.1,
|
432 |
-
# value=1.0,
|
433 |
-
# label="Top P",
|
434 |
-
# render=False,
|
435 |
-
# ),
|
436 |
-
# gr.Slider(
|
437 |
-
# minimum=1,
|
438 |
-
# maximum=20,
|
439 |
-
# step=1,
|
440 |
-
# value=20,
|
441 |
-
# label="Top K",
|
442 |
-
# render=False,
|
443 |
-
# ),
|
444 |
-
# gr.Slider(
|
445 |
-
# minimum=0.0,
|
446 |
-
# maximum=2.0,
|
447 |
-
# step=0.1,
|
448 |
-
# value=1.2,
|
449 |
-
# label="Repetition Penalty",
|
450 |
-
# render=False,
|
451 |
-
# ),
|
452 |
-
# ],
|
453 |
-
# )
|
454 |
-
|
455 |
-
# with gr.Tab("Process PDF"):
|
456 |
-
# pdf_input = gr.File(label="Upload PDF File")
|
457 |
-
# pdf_output = gr.Textbox(label="PDF Result", interactive=False)
|
458 |
-
|
459 |
-
# pdf_button = gr.Button("Process PDF")
|
460 |
-
# pdf_button.click(
|
461 |
-
# process_pdf,
|
462 |
-
# inputs=[pdf_input],
|
463 |
-
# outputs=pdf_output
|
464 |
-
# )
|
465 |
-
|
466 |
-
# with gr.Tab("Search"):
|
467 |
-
# query_input = gr.Textbox(label="Enter Search Query")
|
468 |
-
# search_output = gr.Textbox(label="Search Confidence Result", interactive=False)
|
469 |
-
|
470 |
-
# search_button = gr.Button("Search")
|
471 |
-
# search_button.click(
|
472 |
-
# search_api,
|
473 |
-
# inputs=query_input,
|
474 |
-
# outputs=search_output
|
475 |
-
# )
|
476 |
-
|
477 |
-
# with gr.Tab("Answer with RAG"):
|
478 |
-
# question_input = gr.Textbox(label="Enter Question for RAG")
|
479 |
-
# rag_output = gr.Textbox(label="RAG Answer Result", interactive=False)
|
480 |
-
|
481 |
-
# rag_button = gr.Button("Get Answer")
|
482 |
-
# rag_button.click(
|
483 |
-
# rag_api,
|
484 |
-
# inputs=question_input,
|
485 |
-
# outputs=rag_output
|
486 |
-
# )
|
487 |
-
|
488 |
-
# # Launch the app
|
489 |
-
# if __name__ == "__main__":
|
490 |
-
# demo.launch()
|
491 |
-
|
492 |
-
|
493 |
-
|
494 |
-
|
495 |
-
|
496 |
-
|
497 |
-
|
498 |
-
|
499 |
-
|
500 |
-
|
501 |
-
|
502 |
-
|
503 |
-
# import gradio as gr
|
504 |
-
# from gradio_client import Client, handle_file
|
505 |
-
# import os
|
506 |
-
|
507 |
-
# # Define your Hugging Face token (make sure to set it as an environment variable)
|
508 |
-
# HF_TOKEN = os.getenv("HF_TOKEN") # Replace with your actual token if not using an environment variable
|
509 |
-
|
510 |
-
# # Initialize the Gradio Client for the specified API
|
511 |
-
# client = Client("on1onmangoes/CNIHUB10724v9", hf_token=HF_TOKEN)
|
512 |
-
|
513 |
-
# # Function to handle chat API call
|
514 |
-
# 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):
|
515 |
-
# response = client.predict(
|
516 |
-
# message=message,
|
517 |
-
# client_name=client_name,
|
518 |
-
# system_prompt=system_prompt,
|
519 |
-
# num_retrieved_docs=num_retrieved_docs,
|
520 |
-
# num_docs_final=num_docs_final,
|
521 |
-
# temperature=temperature,
|
522 |
-
# max_new_tokens=max_new_tokens,
|
523 |
-
# top_p=top_p,
|
524 |
-
# top_k=top_k,
|
525 |
-
# penalty=penalty,
|
526 |
-
# api_name="/chat"
|
527 |
-
# )
|
528 |
-
# return response
|
529 |
-
|
530 |
-
# # Function to handle PDF processing API call
|
531 |
-
# def process_pdf(pdf_file, client_name):
|
532 |
-
# return client.predict(
|
533 |
-
# pdf_file=handle_file(pdf_file),
|
534 |
-
# client_name=client_name,
|
535 |
-
# api_name="/process_pdf2"
|
536 |
-
# )[1] # Return only the result string
|
537 |
-
|
538 |
-
# # Function to handle search API call
|
539 |
-
# def search_api(query):
|
540 |
-
# return client.predict(query=query, api_name="/search_with_confidence")
|
541 |
-
|
542 |
-
# # Function to handle RAG API call
|
543 |
-
# def rag_api(question):
|
544 |
-
# return client.predict(question=question, api_name="/answer_with_rag")
|
545 |
-
|
546 |
-
# # Create the Gradio Blocks interface
|
547 |
-
# with gr.Blocks() as app:
|
548 |
-
# gr.Markdown("### Login")
|
549 |
-
|
550 |
-
# with gr.Row():
|
551 |
-
# username_input = gr.Textbox(label="Username", placeholder="Enter your username")
|
552 |
-
# password_input = gr.Textbox(label="Password", placeholder="Enter your password", type="password")
|
553 |
-
|
554 |
-
# with gr.Tab("Chat"):
|
555 |
-
# chatbot = gr.Chatbot() # Create a chatbot interface
|
556 |
-
|
557 |
-
# chat_interface = gr.ChatInterface(
|
558 |
-
# fn=stream_chat_with_rag,
|
559 |
-
# chatbot=chatbot,
|
560 |
-
# fill_height=True,
|
561 |
-
# additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
|
562 |
-
# additional_inputs=[
|
563 |
-
# gr.Dropdown(
|
564 |
-
# ['rosariarossi', 'bianchifiordaliso', 'lorenzoverdi'],
|
565 |
-
# value="rosariarossi",
|
566 |
-
# label="Select Client",
|
567 |
-
# render=False,
|
568 |
-
# ),
|
569 |
-
# gr.Textbox(
|
570 |
-
# value="You are an expert assistant",
|
571 |
-
# label="System Prompt",
|
572 |
-
# render=False,
|
573 |
-
# ),
|
574 |
-
# gr.Slider(
|
575 |
-
# minimum=1,
|
576 |
-
# maximum=10,
|
577 |
-
# step=1,
|
578 |
-
# value=10,
|
579 |
-
# label="Number of Initial Documents to Retrieve",
|
580 |
-
# render=False,
|
581 |
-
# ),
|
582 |
-
# gr.Slider(
|
583 |
-
# minimum=1,
|
584 |
-
# maximum=10,
|
585 |
-
# step=1,
|
586 |
-
# value=9,
|
587 |
-
# label="Number of Final Documents to Retrieve",
|
588 |
-
# render=False,
|
589 |
-
# ),
|
590 |
-
# gr.Slider(
|
591 |
-
# minimum=0.2,
|
592 |
-
# maximum=1,
|
593 |
-
# step=0.1,
|
594 |
-
# value=0,
|
595 |
-
# label="Temperature",
|
596 |
-
# render=False,
|
597 |
-
# ),
|
598 |
-
# gr.Slider(
|
599 |
-
# minimum=128,
|
600 |
-
# maximum=8192,
|
601 |
-
# step=1,
|
602 |
-
# value=1024,
|
603 |
-
# label="Max new tokens",
|
604 |
-
# render=False,
|
605 |
-
# ),
|
606 |
-
# gr.Slider(
|
607 |
-
# minimum=0.0,
|
608 |
-
# maximum=1.0,
|
609 |
-
# step=0.1,
|
610 |
-
# value=1.0,
|
611 |
-
# label="Top P",
|
612 |
-
# render=False,
|
613 |
-
# ),
|
614 |
-
# gr.Slider(
|
615 |
-
# minimum=1,
|
616 |
-
# maximum=20,
|
617 |
-
# step=1,
|
618 |
-
# value=20,
|
619 |
-
# label="Top K",
|
620 |
-
# render=False,
|
621 |
-
# ),
|
622 |
-
# gr.Slider(
|
623 |
-
# minimum=0.0,
|
624 |
-
# maximum=2.0,
|
625 |
-
# step=0.1,
|
626 |
-
# value=1.2,
|
627 |
-
# label="Repetition Penalty",
|
628 |
-
# render=False,
|
629 |
-
# ),
|
630 |
-
# ],
|
631 |
-
# )
|
632 |
-
|
633 |
-
# with gr.Tab("Process PDF"):
|
634 |
-
# pdf_input = gr.File(label="Upload PDF File")
|
635 |
-
# pdf_output = gr.Textbox(label="PDF Result", interactive=False)
|
636 |
-
|
637 |
-
# pdf_button = gr.Button("Process PDF")
|
638 |
-
# pdf_button.click(
|
639 |
-
# process_pdf,
|
640 |
-
# inputs=[pdf_input, client_name_dropdown],
|
641 |
-
# outputs=pdf_output
|
642 |
-
# )
|
643 |
-
|
644 |
-
# with gr.Tab("Search"):
|
645 |
-
# query_input = gr.Textbox(label="Enter Search Query")
|
646 |
-
# search_output = gr.Textbox(label="Search Confidence Result", interactive=False)
|
647 |
-
|
648 |
-
# search_button = gr.Button("Search")
|
649 |
-
# search_button.click(
|
650 |
-
# search_api,
|
651 |
-
# inputs=query_input,
|
652 |
-
# outputs=search_output
|
653 |
-
# )
|
654 |
-
|
655 |
-
# with gr.Tab("Answer with RAG"):
|
656 |
-
# question_input = gr.Textbox(label="Enter Question for RAG")
|
657 |
-
# rag_output = gr.Textbox(label="RAG Answer Result", interactive=False)
|
658 |
-
|
659 |
-
# rag_button = gr.Button("Get Answer")
|
660 |
-
# rag_button.click(
|
661 |
-
# rag_api,
|
662 |
-
# inputs=question_input,
|
663 |
-
# outputs=rag_output
|
664 |
-
# )
|
665 |
-
|
666 |
-
# # Launch the app
|
667 |
-
# app.launch()
|
668 |
-
|
669 |
-
|
670 |
-
|
671 |
-
|
672 |
-
|
673 |
-
|
674 |
-
|
675 |
-
|
676 |
-
|
677 |
-
|
678 |
-
# import gradio as gr
|
679 |
-
# from gradio_client import Client, handle_file
|
680 |
-
# import os
|
681 |
-
|
682 |
-
# # Define your Hugging Face token (make sure to set it as an environment variable)
|
683 |
-
# HF_TOKEN = os.getenv("HF_TOKEN") # Replace with your actual token if not using env variable
|
684 |
-
|
685 |
-
# # Initialize the Gradio Client for the specified API
|
686 |
-
# client = Client("on1onmangoes/CNIHUB10724v9", hf_token=HF_TOKEN)
|
687 |
-
|
688 |
-
# # Authentication function
|
689 |
-
# def login(username, password):
|
690 |
-
# if username == "your_username" and password == "your_password": # Update with actual credentials
|
691 |
-
# return True
|
692 |
-
# else:
|
693 |
-
# return False
|
694 |
-
|
695 |
-
# # Function to handle different API calls based on user input
|
696 |
-
# def handle_api_call(username, password, message=None, client_name="rosariarossi",
|
697 |
-
# system_prompt="You are an expert assistant", num_retrieved_docs=10,
|
698 |
-
# num_docs_final=9, temperature=0, max_new_tokens=1024,
|
699 |
-
# top_p=1, top_k=20, penalty=1.2,
|
700 |
-
# pdf_file=None, query=None, question=None):
|
701 |
-
|
702 |
-
# if not login(username, password):
|
703 |
-
# return "Invalid credentials! Please try again."
|
704 |
-
|
705 |
-
# if message:
|
706 |
-
# # Handle chat message
|
707 |
-
# chat_result = client.predict(
|
708 |
-
# message=message,
|
709 |
-
# client_name=client_name,
|
710 |
-
# system_prompt=system_prompt,
|
711 |
-
# num_retrieved_docs=num_retrieved_docs,
|
712 |
-
# num_docs_final=num_docs_final,
|
713 |
-
# temperature=temperature,
|
714 |
-
# max_new_tokens=max_new_tokens,
|
715 |
-
# top_p=top_p,
|
716 |
-
# top_k=top_k,
|
717 |
-
# penalty=penalty,
|
718 |
-
# api_name="/chat"
|
719 |
-
# )
|
720 |
-
# return chat_result
|
721 |
-
# elif pdf_file:
|
722 |
-
# # Handle PDF file
|
723 |
-
# pdf_result = client.predict(
|
724 |
-
# pdf_file=handle_file(pdf_file),
|
725 |
-
# client_name=client_name,
|
726 |
-
# api_name="/process_pdf2"
|
727 |
-
# )
|
728 |
-
# return pdf_result[1] # Returning the string result from the PDF processing
|
729 |
-
# elif query:
|
730 |
-
# # Handle search query
|
731 |
-
# search_result = client.predict(query=query, api_name="/search_with_confidence")
|
732 |
-
# return search_result
|
733 |
-
# elif question:
|
734 |
-
# # Handle question for RAG
|
735 |
-
# rag_result = client.predict(question=question, api_name="/answer_with_rag")
|
736 |
-
# return rag_result
|
737 |
-
# else:
|
738 |
-
# return "No valid input provided!"
|
739 |
-
|
740 |
-
# # Create the Gradio Blocks interface
|
741 |
-
# with gr.Blocks() as app:
|
742 |
-
# gr.Markdown("### Login")
|
743 |
-
|
744 |
-
# with gr.Row():
|
745 |
-
# username_input = gr.Textbox(label="Username", placeholder="Enter your username")
|
746 |
-
# password_input = gr.Textbox(label="Password", placeholder="Enter your password", type="password")
|
747 |
-
|
748 |
-
# with gr.Tab("Chat"):
|
749 |
-
# message_input = gr.Textbox(label="Message", placeholder="Type your message here")
|
750 |
-
|
751 |
-
# gr.Markdown("### Client Options")
|
752 |
-
# client_name_dropdown = gr.Dropdown(
|
753 |
-
# label="Select Client",
|
754 |
-
# choices=["rosariarossi", "bianchifiordaliso", "lorenzoverdi"],
|
755 |
-
# value="rosariarossi"
|
756 |
-
# )
|
757 |
-
|
758 |
-
# system_prompt_input = gr.Textbox(
|
759 |
-
# label="System Prompt",
|
760 |
-
# placeholder="Enter system prompt here",
|
761 |
-
# value="You are an expert assistant"
|
762 |
-
# )
|
763 |
-
|
764 |
-
# num_retrieved_docs_slider = gr.Slider(
|
765 |
-
# label="Number of Initial Documents to Retrieve",
|
766 |
-
# minimum=1,
|
767 |
-
# maximum=100,
|
768 |
-
# step=1,
|
769 |
-
# value=10
|
770 |
-
# )
|
771 |
-
|
772 |
-
# num_docs_final_slider = gr.Slider(
|
773 |
-
# label="Number of Final Documents to Retrieve",
|
774 |
-
# minimum=1,
|
775 |
-
# maximum=100,
|
776 |
-
# step=1,
|
777 |
-
# value=9
|
778 |
-
# )
|
779 |
-
|
780 |
-
# temperature_slider = gr.Slider(
|
781 |
-
# label="Temperature",
|
782 |
-
# minimum=0,
|
783 |
-
# maximum=2,
|
784 |
-
# step=0.1,
|
785 |
-
# value=0
|
786 |
-
# )
|
787 |
-
|
788 |
-
# max_new_tokens_slider = gr.Slider(
|
789 |
-
# label="Max New Tokens",
|
790 |
-
# minimum=1,
|
791 |
-
# maximum=2048,
|
792 |
-
# step=1,
|
793 |
-
# value=1024
|
794 |
-
# )
|
795 |
-
|
796 |
-
# top_p_slider = gr.Slider(
|
797 |
-
# label="Top P",
|
798 |
-
# minimum=0,
|
799 |
-
# maximum=1,
|
800 |
-
# step=0.01,
|
801 |
-
# value=1
|
802 |
-
# )
|
803 |
-
|
804 |
-
# top_k_slider = gr.Slider(
|
805 |
-
# label="Top K",
|
806 |
-
# minimum=1,
|
807 |
-
# maximum=100,
|
808 |
-
# step=1,
|
809 |
-
# value=20
|
810 |
-
# )
|
811 |
-
|
812 |
-
# penalty_slider = gr.Slider(
|
813 |
-
# label="Repetition Penalty",
|
814 |
-
# minimum=1,
|
815 |
-
# maximum=5,
|
816 |
-
# step=0.1,
|
817 |
-
# value=1.2
|
818 |
-
# )
|
819 |
-
|
820 |
-
# chat_output = gr.Textbox(label="Chat Response", interactive=False)
|
821 |
-
|
822 |
-
# with gr.Tab("Process PDF"):
|
823 |
-
# pdf_input = gr.File(label="Upload PDF File")
|
824 |
-
# pdf_output = gr.Textbox(label="PDF Result", interactive=False)
|
825 |
-
|
826 |
-
# with gr.Tab("Search"):
|
827 |
-
# query_input = gr.Textbox(label="Enter Search Query")
|
828 |
-
# search_output = gr.Textbox(label="Search Confidence Result", interactive=False)
|
829 |
-
|
830 |
-
# with gr.Tab("Answer with RAG"):
|
831 |
-
# question_input = gr.Textbox(label="Enter Question for RAG")
|
832 |
-
# rag_output = gr.Textbox(label="RAG Answer Result", interactive=False)
|
833 |
-
|
834 |
-
# api_button = gr.Button("Submit")
|
835 |
-
|
836 |
-
# # Bind the button click to the handle_api_call function
|
837 |
-
# api_button.click(
|
838 |
-
# handle_api_call,
|
839 |
-
# inputs=[
|
840 |
-
# username_input, password_input,
|
841 |
-
# message_input, client_name_dropdown,
|
842 |
-
# system_prompt_input, num_retrieved_docs_slider,
|
843 |
-
# num_docs_final_slider, temperature_slider,
|
844 |
-
# max_new_tokens_slider, top_p_slider,
|
845 |
-
# top_k_slider, penalty_slider,
|
846 |
-
# pdf_input, query_input, question_input
|
847 |
-
# ],
|
848 |
-
# outputs=[
|
849 |
-
# chat_output, pdf_output, search_output, rag_output
|
850 |
-
# ]
|
851 |
-
# )
|
852 |
-
|
853 |
-
# # Launch the app
|
854 |
-
# app.launch()
|
855 |
-
|
856 |
-
|
857 |
-
|
858 |
-
|
859 |
-
|
860 |
-
|
861 |
-
|
862 |
-
# import gradio as gr
|
863 |
-
# from gradio_client import Client, handle_file
|
864 |
-
# import os
|
865 |
-
|
866 |
-
# # Define your Hugging Face token (make sure to set it as an environment variable)
|
867 |
-
# HF_TOKEN = os.getenv("HF_TOKEN") # Replace with your actual token if not using env variable
|
868 |
-
|
869 |
-
# # Initialize the Gradio Client for the specified API
|
870 |
-
# client = Client("on1onmangoes/CNIHUB10724v9", hf_token=HF_TOKEN)
|
871 |
-
|
872 |
-
# # Authentication function
|
873 |
-
# def login(username, password):
|
874 |
-
# if username == "your_username" and password == "your_password": # Update with actual credentials
|
875 |
-
# return True
|
876 |
-
# else:
|
877 |
-
# return False
|
878 |
-
|
879 |
-
# # Function to handle different API calls based on user input
|
880 |
-
# def handle_api_call(username, password, audio_file=None, pdf_file=None, message=None, query=None, question=None):
|
881 |
-
# if not login(username, password):
|
882 |
-
# return "Invalid credentials! Please try again."
|
883 |
-
|
884 |
-
# if audio_file:
|
885 |
-
# # Handle audio file using the appropriate API
|
886 |
-
# result = client.predict(audio=handle_file(audio_file), api_name="/process_audio") # Example endpoint for audio processing
|
887 |
-
# return result
|
888 |
-
# elif pdf_file:
|
889 |
-
# # Handle PDF file
|
890 |
-
# pdf_result = client.predict(pdf_file=handle_file(pdf_file), client_name="rosariarossi", api_name="/process_pdf2")
|
891 |
-
# return pdf_result[1] # Returning the string result from the PDF processing
|
892 |
-
# elif message:
|
893 |
-
# # Handle chat message
|
894 |
-
# chat_result = client.predict(
|
895 |
-
# message=message,
|
896 |
-
# client_name="rosariarossi",
|
897 |
-
# system_prompt="You are an expert assistant",
|
898 |
-
# num_retrieved_docs=10,
|
899 |
-
# num_docs_final=9,
|
900 |
-
# temperature=0,
|
901 |
-
# max_new_tokens=1024,
|
902 |
-
# top_p=1,
|
903 |
-
# top_k=20,
|
904 |
-
# penalty=1.2,
|
905 |
-
# api_name="/chat"
|
906 |
-
# )
|
907 |
-
# return chat_result
|
908 |
-
# elif query:
|
909 |
-
# # Handle search query
|
910 |
-
# search_result = client.predict(query=query, api_name="/search_with_confidence")
|
911 |
-
# return search_result
|
912 |
-
# elif question:
|
913 |
-
# # Handle question for RAG
|
914 |
-
# rag_result = client.predict(question=question, api_name="/answer_with_rag")
|
915 |
-
# return rag_result
|
916 |
-
# else:
|
917 |
-
# return "No valid input provided!"
|
918 |
-
|
919 |
-
# # Create the Gradio Blocks interface
|
920 |
-
# with gr.Blocks() as app:
|
921 |
-
# gr.Markdown("### Login")
|
922 |
-
|
923 |
-
# with gr.Row():
|
924 |
-
# username_input = gr.Textbox(label="Username", placeholder="Enter your username")
|
925 |
-
# password_input = gr.Textbox(label="Password", placeholder="Enter your password", type="password")
|
926 |
-
|
927 |
-
# audio_input = gr.Audio(label="Upload Audio File", type="filepath")
|
928 |
-
# pdf_input = gr.File(label="Upload PDF File")
|
929 |
-
|
930 |
-
# message_input = gr.Textbox(label="Enter Message for Chat")
|
931 |
-
# query_input = gr.Textbox(label="Enter Search Query")
|
932 |
-
# question_input = gr.Textbox(label="Enter Question for RAG")
|
933 |
-
|
934 |
-
# output_text = gr.Textbox(label="Output", interactive=False)
|
935 |
-
|
936 |
-
# # Bind the button click to the handle_api_call function
|
937 |
-
# api_button = gr.Button("Submit")
|
938 |
-
# api_button.click(
|
939 |
-
# handle_api_call,
|
940 |
-
# inputs=[username_input, password_input, audio_input, pdf_input, message_input, query_input, question_input],
|
941 |
-
# outputs=output_text
|
942 |
-
# )
|
943 |
-
|
944 |
-
# # Launch the app
|
945 |
-
# app.launch()
|
946 |
-
|
947 |
-
|
948 |
-
|
949 |
-
|
950 |
-
|
951 |
-
# import gradio as gr
|
952 |
-
|
953 |
-
# # Define a function for the main application
|
954 |
-
# def greet(name):
|
955 |
-
# return f"Hello {name}!"
|
956 |
-
|
957 |
-
# # Define a function for the authentication
|
958 |
-
# def login(username, password):
|
959 |
-
# if username == "your_username" and password == "your_password":
|
960 |
-
# return True
|
961 |
-
# else:
|
962 |
-
# return False
|
963 |
-
|
964 |
-
# # Create the Gradio Blocks interface
|
965 |
-
# with gr.Blocks() as app:
|
966 |
-
# gr.Markdown("### Login")
|
967 |
-
|
968 |
-
# with gr.Row():
|
969 |
-
# username_input = gr.Textbox(label="Username", placeholder="Enter your username")
|
970 |
-
# password_input = gr.Textbox(label="Password", placeholder="Enter your password", type="password")
|
971 |
-
|
972 |
-
# login_button = gr.Button("Login")
|
973 |
-
# output_text = gr.Textbox(label="Output", interactive=False)
|
974 |
-
|
975 |
-
# # Function to handle login and display greeting
|
976 |
-
# def handle_login(username, password):
|
977 |
-
# if login(username, password):
|
978 |
-
# # Clear the password field and display the greeting
|
979 |
-
# #password_input.clear()
|
980 |
-
# return greet(username)
|
981 |
-
# else:
|
982 |
-
# return "Invalid credentials! Please try again."
|
983 |
-
|
984 |
-
# # Bind the button click to the handle_login function
|
985 |
-
# login_button.click(handle_login, inputs=[username_input, password_input], outputs=output_text)
|
986 |
-
|
987 |
-
# # Launch the app
|
988 |
-
# app.launch()
|
989 |
-
|
990 |
-
|
|
|
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/CNIHUB10724v10", hf_token=HF_TOKEN)
|
10 |
|
11 |
|
12 |
# Function to handle chat API call
|
|
|
40 |
# Return the assistant's reply
|
41 |
return response
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
# Function to handle PDF processing API call
|
44 |
def process_pdf(pdf_file):
|
45 |
return client.predict(
|
|
|
69 |
# Create the Gradio Blocks interface
|
70 |
with gr.Blocks(css=CSS) as demo:
|
71 |
gr.HTML(TITLE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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