Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
|
|
2 |
import requests
|
3 |
from fastapi import FastAPI
|
4 |
from fastapi.testclient import TestClient
|
5 |
-
from routes import
|
6 |
|
7 |
app = FastAPI()
|
8 |
|
@@ -15,13 +15,16 @@ app.include_router(customer_support.router,
|
|
15 |
prefix="/customer-support", tags=["customer-support"])
|
16 |
app.include_router(search_products.router,
|
17 |
prefix="/search-products", tags=["search-products"])
|
18 |
-
app.include_router(
|
19 |
-
prefix="/
|
20 |
|
21 |
# Initialize the TestClient with the FastAPI app
|
22 |
client = TestClient(app)
|
23 |
|
24 |
-
def
|
|
|
|
|
|
|
25 |
|
26 |
history_openai_format = [
|
27 |
{"role": "system", "content": "You are an assistant for an eCommerce store."}]
|
@@ -29,30 +32,62 @@ def fastlane_agent(message, history):
|
|
29 |
history_openai_format.append({"role": "user", "content": human})
|
30 |
history_openai_format.append(
|
31 |
{"role": "assistant", "content": assistant})
|
32 |
-
history_openai_format.append({"role": "user", "content": message})
|
33 |
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
if response.status_code == 200:
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import requests
|
3 |
from fastapi import FastAPI
|
4 |
from fastapi.testclient import TestClient
|
5 |
+
from routes import input_handler, purchase, order_management, account_management, customer_support, search_products
|
6 |
|
7 |
app = FastAPI()
|
8 |
|
|
|
15 |
prefix="/customer-support", tags=["customer-support"])
|
16 |
app.include_router(search_products.router,
|
17 |
prefix="/search-products", tags=["search-products"])
|
18 |
+
app.include_router(input_handler.router,
|
19 |
+
prefix="/input-handler", tags=["input-handler"])
|
20 |
|
21 |
# Initialize the TestClient with the FastAPI app
|
22 |
client = TestClient(app)
|
23 |
|
24 |
+
def add_message(history, message, audio_input):
|
25 |
+
|
26 |
+
if message is None and audio_input is None:
|
27 |
+
return "Please provide either text or audio."
|
28 |
|
29 |
history_openai_format = [
|
30 |
{"role": "system", "content": "You are an assistant for an eCommerce store."}]
|
|
|
32 |
history_openai_format.append({"role": "user", "content": human})
|
33 |
history_openai_format.append(
|
34 |
{"role": "assistant", "content": assistant})
|
|
|
35 |
|
36 |
+
if message["text"] is not None:
|
37 |
+
history_openai_format.append(
|
38 |
+
{"role": "user", "content": message["text"]})
|
39 |
+
|
40 |
+
if audio_input:
|
41 |
+
transcription = transcribe(audio_input)
|
42 |
+
print(f"Transcription: {transcription}")
|
43 |
+
message["text"] += f' [Audio transcription] = {transcription}'
|
44 |
+
history_openai_format.append(
|
45 |
+
{"role": "user", "content": transcription})
|
46 |
+
|
47 |
+
for x in message["files"]:
|
48 |
+
#history.append(((x,), None))
|
49 |
+
message["text"] += ' [File attached]'
|
50 |
+
history_openai_format.append(
|
51 |
+
{"role": "user", "content": "Image attached"})
|
52 |
+
|
53 |
+
response = requests.post(
|
54 |
+
"http://localhost:8000/input-handler/", json={"text": message["text"], "files": message["files"], "history": history_openai_format})
|
55 |
+
|
56 |
if response.status_code == 200:
|
57 |
+
bot_response = response.json().get("generative response")
|
58 |
+
history.append((message["text"], bot_response))
|
59 |
+
|
60 |
+
return history, gr.MultimodalTextbox(value=None, interactive=False), None
|
61 |
+
|
62 |
+
with gr.Blocks(theme="soft") as demo:
|
63 |
+
gr.Markdown("<h1 style='text-align: center;'>Fastlane Chat GPT</h1>")
|
64 |
+
gr.Markdown("AI sales assistance for e-commerce")
|
65 |
+
|
66 |
+
chatbot = gr.Chatbot(
|
67 |
+
height=400,
|
68 |
+
elem_id="chatbot"
|
69 |
+
)
|
70 |
+
|
71 |
+
# Add clear and undo buttons
|
72 |
+
with gr.Row():
|
73 |
+
undo_btn = gr.Button("Delete Previous")
|
74 |
+
clear_btn = gr.Button("Clear")
|
75 |
+
|
76 |
+
undo_btn.click(undo_last_message, chatbot, chatbot)
|
77 |
+
clear_btn.click(clear_chat, [], chatbot)
|
78 |
+
|
79 |
+
chat_input = gr.MultimodalTextbox(
|
80 |
+
interactive=True, placeholder="Enter message, upload file, or record audio...", show_label=False)
|
81 |
+
audio_input = gr.Audio(sources=["microphone"])
|
82 |
+
|
83 |
+
chat_msg = chat_input.submit(
|
84 |
+
add_message, [chatbot, chat_input, audio_input], [chatbot, chat_input, audio_input])
|
85 |
+
chat_msg.then(lambda: gr.MultimodalTextbox(
|
86 |
+
interactive=True), None, [chat_input])
|
87 |
+
|
88 |
+
chatbot.like(print_like_dislike, None, None)
|
89 |
+
|
90 |
+
demo.queue()
|
91 |
+
demo.launch()
|
92 |
+
|
93 |
+
app = gr.mount_gradio_app(app, demo, path="./")
|