Spaces:
Runtime error
Runtime error
fix
Browse files- app.py +78 -28
- utils/__init__.py +2 -0
- utils/__pycache__/__init__.cpython-310.pyc +0 -0
- utils/__pycache__/bot.cpython-310.pyc +0 -0
- utils/functions.py +38 -0
app.py
CHANGED
@@ -1,36 +1,86 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
from utils import Bot
|
|
|
3 |
|
4 |
-
def
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
)
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
28 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
|
|
31 |
|
32 |
-
submit_init_button, submit_chat_button = chatbot_interface()
|
33 |
|
34 |
-
submit_init_button.launch()
|
35 |
-
submit_chat_button.launch()
|
36 |
|
|
|
1 |
import gradio as gr
|
2 |
+
import time
|
3 |
from utils import Bot
|
4 |
+
from utils import make_documens, make_descriptions
|
5 |
|
6 |
+
def init_bot(table=None,tittle=None,pdf=None,key=None):
|
7 |
+
|
8 |
+
if key is None:
|
9 |
+
return 'You must submit OpenAI key'
|
10 |
+
|
11 |
+
if pdf is None:
|
12 |
+
return 'You must submit pdf file'
|
13 |
+
|
14 |
+
if table is None:
|
15 |
+
return 'You must submit table file'
|
16 |
+
|
17 |
+
if tittle is None:
|
18 |
+
return 'You must submit tittle of table'
|
19 |
+
|
20 |
+
table = table.name
|
21 |
+
pdf = pdf.name
|
22 |
+
table_descriptions = make_descriptions(tittle=tittle,table=table)
|
23 |
+
documents = make_documens(pdf)
|
24 |
+
|
25 |
+
print(documents[0])
|
26 |
+
global bot
|
27 |
+
|
28 |
+
bot = Bot(
|
29 |
+
openai_api_key=key,
|
30 |
+
table_descriptions=table_descriptions,
|
31 |
+
text_documents=documents,
|
32 |
+
verbose=False
|
33 |
)
|
34 |
+
|
35 |
+
return 'Chat bot successfully initialized'
|
36 |
+
|
37 |
+
def msg_bot(history):
|
38 |
+
message = history[-1][0]
|
39 |
+
|
40 |
+
bot_message = bot(message)['output']
|
41 |
+
history[-1][1] = ""
|
42 |
+
for character in bot_message:
|
43 |
+
history[-1][1] += character
|
44 |
+
time.sleep(0.05)
|
45 |
+
yield history
|
46 |
+
|
47 |
+
def user(user_message, history):
|
48 |
+
return "", history + [[user_message, None]]
|
49 |
+
|
50 |
+
with gr.Blocks() as demo:
|
51 |
+
|
52 |
+
with gr.Tab("Chat bot initialization"):
|
53 |
+
|
54 |
+
key = gr.Textbox(label='OpenAI key')
|
55 |
+
|
56 |
+
with gr.Row(variant='panel'):
|
57 |
+
with gr.Column():
|
58 |
+
with gr.Row():
|
59 |
+
title = gr.Textbox(label='Table tittle')
|
60 |
+
with gr.Row():
|
61 |
+
table = gr.File(label='csv table')
|
62 |
+
|
63 |
+
pdf = gr.File(label='pdf')
|
64 |
+
with gr.Row(variant='panel'):
|
65 |
+
|
66 |
+
init_button = gr.Button('submit')
|
67 |
+
init_output = gr.Textbox(label="Initialization status")
|
68 |
+
init_button.click(fn=init_bot,inputs=[table,title,pdf,key],outputs=init_output,api_name='init')
|
69 |
+
|
70 |
+
|
71 |
+
|
72 |
+
with gr.Tab("Chat bot"):
|
73 |
+
|
74 |
+
chatbot = gr.Chatbot()
|
75 |
+
msg = gr.Textbox(label='Ask the bot')
|
76 |
+
clear = gr.Button('Clear')
|
77 |
+
msg.submit(user,[msg,chatbot],[msg,chatbot],queue=False).then(
|
78 |
+
msg_bot, chatbot, chatbot
|
79 |
+
)
|
80 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
81 |
|
82 |
+
demo.queue()
|
83 |
+
demo.launch()
|
84 |
|
|
|
85 |
|
|
|
|
|
86 |
|
utils/__init__.py
CHANGED
@@ -1 +1,3 @@
|
|
1 |
from .bot import Bot
|
|
|
|
|
|
1 |
from .bot import Bot
|
2 |
+
from .functions import make_documens, make_descriptions
|
3 |
+
|
utils/__pycache__/__init__.cpython-310.pyc
DELETED
Binary file (166 Bytes)
|
|
utils/__pycache__/bot.cpython-310.pyc
DELETED
Binary file (5.72 kB)
|
|
utils/functions.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import pandas as pd
|
3 |
+
from langchain.document_loaders import PyPDFLoader
|
4 |
+
from langchain.text_splitter import CharacterTextSplitter
|
5 |
+
|
6 |
+
|
7 |
+
def make_descriptions(table, tittle):
|
8 |
+
|
9 |
+
df = pd.read_csv(table)
|
10 |
+
columns = list(df.columns)
|
11 |
+
|
12 |
+
table_description0 = {
|
13 |
+
'path': 'random',
|
14 |
+
'number': 1,
|
15 |
+
'columns': ["clothes", "animals", "students"],
|
16 |
+
'tittle': "fashionable student clothes"
|
17 |
+
}
|
18 |
+
|
19 |
+
table_description1 = {
|
20 |
+
'path': table,
|
21 |
+
'number': 2,
|
22 |
+
'columns': columns,
|
23 |
+
'tittle': tittle
|
24 |
+
}
|
25 |
+
|
26 |
+
table_descriptions = [table_description0, table_description1]
|
27 |
+
return table_descriptions
|
28 |
+
|
29 |
+
|
30 |
+
def make_documens(pdf):
|
31 |
+
loader = PyPDFLoader(pdf)
|
32 |
+
documents = loader.load()
|
33 |
+
|
34 |
+
text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=0, separator='\n')
|
35 |
+
documents = text_splitter.split_documents(documents)
|
36 |
+
return documents
|
37 |
+
|
38 |
+
|