Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,13 @@ import gradio
|
|
8 |
from gradio_client import Client
|
9 |
import autogen
|
10 |
import panel as pn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
from autogen_utils import (
|
12 |
MathUserProxyAgent,
|
13 |
RetrieveUserProxyAgent,
|
@@ -21,6 +28,29 @@ from custom_widgets import RowAgentWidget
|
|
21 |
from panel.chat import ChatInterface
|
22 |
from panel.widgets import Button, CodeEditor, PasswordInput, Switch, TextInput
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
pn.extension("codeeditor")
|
25 |
template = pn.template.BootstrapTemplate(title=TITLE)
|
26 |
|
@@ -316,7 +346,18 @@ def init_groupchat(event, collection_name):
|
|
316 |
return agents, manager, groupchat
|
317 |
|
318 |
|
319 |
-
async def agents_chat(init_sender, manager, contents, agents):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
320 |
recipient = (
|
321 |
manager
|
322 |
if len(agents) > 2
|
@@ -324,6 +365,8 @@ async def agents_chat(init_sender, manager, contents, agents):
|
|
324 |
if agents[1] != init_sender
|
325 |
else agents[0]
|
326 |
)
|
|
|
|
|
327 |
if isinstance(init_sender, (RetrieveUserProxyAgent, MathUserProxyAgent)):
|
328 |
await init_sender.a_initiate_chat(recipient, problem=contents)
|
329 |
else:
|
|
|
8 |
from gradio_client import Client
|
9 |
import autogen
|
10 |
import panel as pn
|
11 |
+
import os
|
12 |
+
from unstructured.ingest.interfaces import PartitionConfig, ProcessorConfig, ReadConfig
|
13 |
+
from unstructured.ingest.runner import LocalRunner
|
14 |
+
from unstructured.partition.auto import partition
|
15 |
+
from langchain.document_loaders import UnstructuredFileLoader
|
16 |
+
|
17 |
+
|
18 |
from autogen_utils import (
|
19 |
MathUserProxyAgent,
|
20 |
RetrieveUserProxyAgent,
|
|
|
28 |
from panel.chat import ChatInterface
|
29 |
from panel.widgets import Button, CodeEditor, PasswordInput, Switch, TextInput
|
30 |
|
31 |
+
def process_file_with_unstructured(file_path):
|
32 |
+
# Set up the LocalRunner
|
33 |
+
runner = LocalRunner(
|
34 |
+
processor_config=ProcessorConfig(
|
35 |
+
verbose=True,
|
36 |
+
output_dir="local-ingest-output",
|
37 |
+
num_processes=2,
|
38 |
+
),
|
39 |
+
read_config=ReadConfig(),
|
40 |
+
partition_config=PartitionConfig(
|
41 |
+
partition_by_api=True,
|
42 |
+
api_key=os.getenv("UNSTRUCTURED_API_KEY"),
|
43 |
+
),
|
44 |
+
)
|
45 |
+
runner.run(input_path=file_path, recursive=True)
|
46 |
+
elements = partition(filename=file_path, content_type="application/pdf")
|
47 |
+
loader = UnstructuredFileLoader(file_path)
|
48 |
+
docs = loader.load()
|
49 |
+
raw_text = docs[0].page_content
|
50 |
+
|
51 |
+
return raw_text
|
52 |
+
|
53 |
+
|
54 |
pn.extension("codeeditor")
|
55 |
template = pn.template.BootstrapTemplate(title=TITLE)
|
56 |
|
|
|
346 |
return agents, manager, groupchat
|
347 |
|
348 |
|
349 |
+
async def agents_chat(init_sender, manager, contents, agents, RAG):
|
350 |
+
# Check if a file is uploaded
|
351 |
+
if RAG.value:
|
352 |
+
# Save the file and process it
|
353 |
+
file_path = "path/to/saved/file" # Define the path to save the uploaded file
|
354 |
+
RAG.save(file_path)
|
355 |
+
raw_text = process_file_with_unstructured(file_path)
|
356 |
+
|
357 |
+
# Prepend the extracted text to the contents
|
358 |
+
contents = raw_text + ' ' + contents
|
359 |
+
|
360 |
+
# Determine the recipient
|
361 |
recipient = (
|
362 |
manager
|
363 |
if len(agents) > 2
|
|
|
365 |
if agents[1] != init_sender
|
366 |
else agents[0]
|
367 |
)
|
368 |
+
|
369 |
+
# Initiate chat
|
370 |
if isinstance(init_sender, (RetrieveUserProxyAgent, MathUserProxyAgent)):
|
371 |
await init_sender.a_initiate_chat(recipient, problem=contents)
|
372 |
else:
|