hrguarinv commited on
Commit
e5b7602
·
verified ·
1 Parent(s): a9e0812

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -29
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 query_handler, purchase, order_management, account_management, customer_support, search_products
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(query_handler.router,
19
- prefix="/query-handler", tags=["query-handler"])
20
 
21
  # Initialize the TestClient with the FastAPI app
22
  client = TestClient(app)
23
 
24
- def fastlane_agent(message, history):
 
 
 
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
- response = client.post("/query-handler/", json={"text": message, "history": history_openai_format})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  if response.status_code == 200:
36
- return response.json().get("generative response")
37
- else:
38
- return "Error: Could not fetch response."
39
-
40
-
41
- iface = gr.ChatInterface(
42
- fn=fastlane_agent,
43
- chatbot=gr.Chatbot(height=400),
44
- textbox=gr.Textbox(
45
- placeholder="How can I help you?", container=False, scale=7
46
- ),
47
- title="Try it!",
48
- description="AI sales assistance for e-commmerce",
49
- theme="soft",
50
- examples=["Hello", "What is the status of my order?",
51
- "Recommend me products"],
52
- cache_examples=True,
53
- retry_btn=None,
54
- undo_btn="Delete Previous",
55
- clear_btn="Clear"
56
- ).launch()
57
-
58
- app = gr.mount_gradio_app(app, iface, path="./")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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="./")