Spaces:
Runtime error
Runtime error
on1onmangoes
commited on
Commit
•
0d9856e
1
Parent(s):
1d6a862
Update app.py
Browse files
app.py
CHANGED
@@ -16,34 +16,39 @@ def login(username, password):
|
|
16 |
return False
|
17 |
|
18 |
# Function to handle different API calls based on user input
|
19 |
-
def handle_api_call(username, password,
|
|
|
|
|
|
|
|
|
|
|
20 |
if not login(username, password):
|
21 |
return "Invalid credentials! Please try again."
|
22 |
|
23 |
-
if
|
24 |
-
# Handle audio file using the appropriate API
|
25 |
-
result = client.predict(audio=handle_file(audio_file), api_name="/process_audio") # Example endpoint for audio processing
|
26 |
-
return result
|
27 |
-
elif pdf_file:
|
28 |
-
# Handle PDF file
|
29 |
-
pdf_result = client.predict(pdf_file=handle_file(pdf_file), client_name="rosariarossi", api_name="/process_pdf2")
|
30 |
-
return pdf_result[1] # Returning the string result from the PDF processing
|
31 |
-
elif message:
|
32 |
# Handle chat message
|
33 |
chat_result = client.predict(
|
34 |
message=message,
|
35 |
-
client_name=
|
36 |
-
system_prompt=
|
37 |
-
num_retrieved_docs=
|
38 |
-
num_docs_final=
|
39 |
-
temperature=
|
40 |
-
max_new_tokens=
|
41 |
-
top_p=
|
42 |
-
top_k=
|
43 |
-
penalty=
|
44 |
api_name="/chat"
|
45 |
)
|
46 |
return chat_result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
elif query:
|
48 |
# Handle search query
|
49 |
search_result = client.predict(query=query, api_name="/search_with_confidence")
|
@@ -63,21 +68,109 @@ with gr.Blocks() as app:
|
|
63 |
username_input = gr.Textbox(label="Username", placeholder="Enter your username")
|
64 |
password_input = gr.Textbox(label="Password", placeholder="Enter your password", type="password")
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
# Bind the button click to the handle_api_call function
|
76 |
api_button = gr.Button("Submit")
|
|
|
|
|
77 |
api_button.click(
|
78 |
handle_api_call,
|
79 |
-
inputs=[
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
)
|
82 |
|
83 |
# Launch the app
|
@@ -87,6 +180,97 @@ app.launch()
|
|
87 |
|
88 |
|
89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
# import gradio as gr
|
91 |
|
92 |
# # Define a function for the main application
|
|
|
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")
|
|
|
68 |
username_input = gr.Textbox(label="Username", placeholder="Enter your username")
|
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
|
|
|
180 |
|
181 |
|
182 |
|
183 |
+
|
184 |
+
|
185 |
+
# import gradio as gr
|
186 |
+
# from gradio_client import Client, handle_file
|
187 |
+
# import os
|
188 |
+
|
189 |
+
# # Define your Hugging Face token (make sure to set it as an environment variable)
|
190 |
+
# HF_TOKEN = os.getenv("HF_TOKEN") # Replace with your actual token if not using env variable
|
191 |
+
|
192 |
+
# # Initialize the Gradio Client for the specified API
|
193 |
+
# client = Client("on1onmangoes/CNIHUB10724v9", hf_token=HF_TOKEN)
|
194 |
+
|
195 |
+
# # Authentication function
|
196 |
+
# def login(username, password):
|
197 |
+
# if username == "your_username" and password == "your_password": # Update with actual credentials
|
198 |
+
# return True
|
199 |
+
# else:
|
200 |
+
# return False
|
201 |
+
|
202 |
+
# # Function to handle different API calls based on user input
|
203 |
+
# def handle_api_call(username, password, audio_file=None, pdf_file=None, message=None, query=None, question=None):
|
204 |
+
# if not login(username, password):
|
205 |
+
# return "Invalid credentials! Please try again."
|
206 |
+
|
207 |
+
# if audio_file:
|
208 |
+
# # Handle audio file using the appropriate API
|
209 |
+
# result = client.predict(audio=handle_file(audio_file), api_name="/process_audio") # Example endpoint for audio processing
|
210 |
+
# return result
|
211 |
+
# elif pdf_file:
|
212 |
+
# # Handle PDF file
|
213 |
+
# pdf_result = client.predict(pdf_file=handle_file(pdf_file), client_name="rosariarossi", api_name="/process_pdf2")
|
214 |
+
# return pdf_result[1] # Returning the string result from the PDF processing
|
215 |
+
# elif message:
|
216 |
+
# # Handle chat message
|
217 |
+
# chat_result = client.predict(
|
218 |
+
# message=message,
|
219 |
+
# client_name="rosariarossi",
|
220 |
+
# system_prompt="You are an expert assistant",
|
221 |
+
# num_retrieved_docs=10,
|
222 |
+
# num_docs_final=9,
|
223 |
+
# temperature=0,
|
224 |
+
# max_new_tokens=1024,
|
225 |
+
# top_p=1,
|
226 |
+
# top_k=20,
|
227 |
+
# penalty=1.2,
|
228 |
+
# api_name="/chat"
|
229 |
+
# )
|
230 |
+
# return chat_result
|
231 |
+
# elif query:
|
232 |
+
# # Handle search query
|
233 |
+
# search_result = client.predict(query=query, api_name="/search_with_confidence")
|
234 |
+
# return search_result
|
235 |
+
# elif question:
|
236 |
+
# # Handle question for RAG
|
237 |
+
# rag_result = client.predict(question=question, api_name="/answer_with_rag")
|
238 |
+
# return rag_result
|
239 |
+
# else:
|
240 |
+
# return "No valid input provided!"
|
241 |
+
|
242 |
+
# # Create the Gradio Blocks interface
|
243 |
+
# with gr.Blocks() as app:
|
244 |
+
# gr.Markdown("### Login")
|
245 |
+
|
246 |
+
# with gr.Row():
|
247 |
+
# username_input = gr.Textbox(label="Username", placeholder="Enter your username")
|
248 |
+
# password_input = gr.Textbox(label="Password", placeholder="Enter your password", type="password")
|
249 |
+
|
250 |
+
# audio_input = gr.Audio(label="Upload Audio File", type="filepath")
|
251 |
+
# pdf_input = gr.File(label="Upload PDF File")
|
252 |
+
|
253 |
+
# message_input = gr.Textbox(label="Enter Message for Chat")
|
254 |
+
# query_input = gr.Textbox(label="Enter Search Query")
|
255 |
+
# question_input = gr.Textbox(label="Enter Question for RAG")
|
256 |
+
|
257 |
+
# output_text = gr.Textbox(label="Output", interactive=False)
|
258 |
+
|
259 |
+
# # Bind the button click to the handle_api_call function
|
260 |
+
# api_button = gr.Button("Submit")
|
261 |
+
# api_button.click(
|
262 |
+
# handle_api_call,
|
263 |
+
# inputs=[username_input, password_input, audio_input, pdf_input, message_input, query_input, question_input],
|
264 |
+
# outputs=output_text
|
265 |
+
# )
|
266 |
+
|
267 |
+
# # Launch the app
|
268 |
+
# app.launch()
|
269 |
+
|
270 |
+
|
271 |
+
|
272 |
+
|
273 |
+
|
274 |
# import gradio as gr
|
275 |
|
276 |
# # Define a function for the main application
|