Spaces:
Runtime error
Runtime error
Update timeout, check file type
Browse files
app.py
CHANGED
|
@@ -1,18 +1,19 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
|
|
|
| 3 |
import shutil
|
| 4 |
import openai
|
| 5 |
import autogen
|
| 6 |
import chromadb
|
| 7 |
import multiprocessing as mp
|
| 8 |
-
from autogen.retrieve_utils import TEXT_FORMATS
|
| 9 |
from autogen.agentchat.contrib.retrieve_assistant_agent import RetrieveAssistantAgent
|
| 10 |
from autogen.agentchat.contrib.retrieve_user_proxy_agent import (
|
| 11 |
RetrieveUserProxyAgent,
|
| 12 |
PROMPT_CODE,
|
| 13 |
)
|
| 14 |
|
| 15 |
-
TIMEOUT =
|
| 16 |
|
| 17 |
def initialize_agents(config_list, docs_path=None):
|
| 18 |
if isinstance(config_list, gr.State):
|
|
@@ -61,6 +62,7 @@ def initiate_chat(config_list, problem, queue, n_results=3):
|
|
| 61 |
"request_timeout": TIMEOUT,
|
| 62 |
"seed": 42,
|
| 63 |
"config_list": _config_list,
|
|
|
|
| 64 |
},
|
| 65 |
)
|
| 66 |
assistant.llm_config.update(llm_config[0])
|
|
@@ -222,14 +224,13 @@ with gr.Blocks() as demo:
|
|
| 222 |
|
| 223 |
with gr.Row():
|
| 224 |
def upload_file(file):
|
| 225 |
-
update_context_url(file.name)
|
| 226 |
|
| 227 |
upload_button = gr.UploadButton(
|
| 228 |
"Click to upload a context file or enter a url in the right textbox",
|
| 229 |
file_types=[f".{i}" for i in TEXT_FORMATS],
|
| 230 |
file_count="single",
|
| 231 |
)
|
| 232 |
-
upload_button.upload(upload_file, upload_button)
|
| 233 |
|
| 234 |
txt_context_url = gr.Textbox(
|
| 235 |
label="Enter the url to your context file and chat on the context",
|
|
@@ -263,19 +264,35 @@ with gr.Blocks() as demo:
|
|
| 263 |
def update_prompt(prompt):
|
| 264 |
ragproxyagent.customized_prompt = prompt
|
| 265 |
return prompt
|
| 266 |
-
|
| 267 |
def update_context_url(context_url):
|
| 268 |
global assistant, ragproxyagent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
try:
|
| 270 |
shutil.rmtree("/tmp/chromadb/")
|
| 271 |
except:
|
| 272 |
pass
|
| 273 |
-
assistant, ragproxyagent = initialize_agents(config_list, docs_path=
|
| 274 |
return context_url
|
| 275 |
|
| 276 |
txt_input.submit(respond, [txt_input, chatbot, txt_model, txt_oai_key, txt_aoai_key, txt_aoai_base_url], [txt_input, chatbot])
|
| 277 |
txt_prompt.submit(update_prompt, [txt_prompt], [txt_prompt])
|
| 278 |
txt_context_url.submit(update_context_url, [txt_context_url], [txt_context_url])
|
|
|
|
| 279 |
|
| 280 |
|
| 281 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
from pathlib import Path
|
| 4 |
import shutil
|
| 5 |
import openai
|
| 6 |
import autogen
|
| 7 |
import chromadb
|
| 8 |
import multiprocessing as mp
|
| 9 |
+
from autogen.retrieve_utils import TEXT_FORMATS, get_file_from_url, is_url
|
| 10 |
from autogen.agentchat.contrib.retrieve_assistant_agent import RetrieveAssistantAgent
|
| 11 |
from autogen.agentchat.contrib.retrieve_user_proxy_agent import (
|
| 12 |
RetrieveUserProxyAgent,
|
| 13 |
PROMPT_CODE,
|
| 14 |
)
|
| 15 |
|
| 16 |
+
TIMEOUT = 60
|
| 17 |
|
| 18 |
def initialize_agents(config_list, docs_path=None):
|
| 19 |
if isinstance(config_list, gr.State):
|
|
|
|
| 62 |
"request_timeout": TIMEOUT,
|
| 63 |
"seed": 42,
|
| 64 |
"config_list": _config_list,
|
| 65 |
+
"use_cache": False,
|
| 66 |
},
|
| 67 |
)
|
| 68 |
assistant.llm_config.update(llm_config[0])
|
|
|
|
| 224 |
|
| 225 |
with gr.Row():
|
| 226 |
def upload_file(file):
|
| 227 |
+
return update_context_url(file.name)
|
| 228 |
|
| 229 |
upload_button = gr.UploadButton(
|
| 230 |
"Click to upload a context file or enter a url in the right textbox",
|
| 231 |
file_types=[f".{i}" for i in TEXT_FORMATS],
|
| 232 |
file_count="single",
|
| 233 |
)
|
|
|
|
| 234 |
|
| 235 |
txt_context_url = gr.Textbox(
|
| 236 |
label="Enter the url to your context file and chat on the context",
|
|
|
|
| 264 |
def update_prompt(prompt):
|
| 265 |
ragproxyagent.customized_prompt = prompt
|
| 266 |
return prompt
|
| 267 |
+
|
| 268 |
def update_context_url(context_url):
|
| 269 |
global assistant, ragproxyagent
|
| 270 |
+
|
| 271 |
+
file_extension = Path(context_url).suffix
|
| 272 |
+
print("file_extension: ", file_extension)
|
| 273 |
+
if file_extension.lower() not in [f'.{i}' for i in TEXT_FORMATS]:
|
| 274 |
+
return f"File must be in the format of {TEXT_FORMATS}"
|
| 275 |
+
|
| 276 |
+
if is_url(context_url):
|
| 277 |
+
try:
|
| 278 |
+
file_path = get_file_from_url(context_url, save_path=os.path.join("/tmp", os.path.basename(context_url)))
|
| 279 |
+
except Exception as e:
|
| 280 |
+
return str(e)
|
| 281 |
+
else:
|
| 282 |
+
file_path = context_url
|
| 283 |
+
context_url = os.path.basename(context_url)
|
| 284 |
+
|
| 285 |
try:
|
| 286 |
shutil.rmtree("/tmp/chromadb/")
|
| 287 |
except:
|
| 288 |
pass
|
| 289 |
+
assistant, ragproxyagent = initialize_agents(config_list, docs_path=file_path)
|
| 290 |
return context_url
|
| 291 |
|
| 292 |
txt_input.submit(respond, [txt_input, chatbot, txt_model, txt_oai_key, txt_aoai_key, txt_aoai_base_url], [txt_input, chatbot])
|
| 293 |
txt_prompt.submit(update_prompt, [txt_prompt], [txt_prompt])
|
| 294 |
txt_context_url.submit(update_context_url, [txt_context_url], [txt_context_url])
|
| 295 |
+
upload_button.upload(upload_file, upload_button, [txt_context_url])
|
| 296 |
|
| 297 |
|
| 298 |
if __name__ == "__main__":
|