|
import gradio as gr |
|
import requests |
|
from fastapi import FastAPI |
|
from fastapi.testclient import TestClient |
|
from routes import query_handler, purchase, order_management, account_management, customer_support, search_products |
|
|
|
app = FastAPI() |
|
|
|
app.include_router(purchase.router, prefix="/purchase", tags=["purchase"]) |
|
app.include_router(order_management.router, |
|
prefix="/order-management", tags=["order-management"]) |
|
app.include_router(account_management.router, |
|
prefix="/account-management", tags=["account-management"]) |
|
app.include_router(customer_support.router, |
|
prefix="/customer-support", tags=["customer-support"]) |
|
app.include_router(search_products.router, |
|
prefix="/search-products", tags=["search-products"]) |
|
app.include_router(query_handler.router, |
|
prefix="/query-handler", tags=["query-handler"]) |
|
|
|
|
|
client = TestClient(app) |
|
|
|
def fastlane_agent(message, history): |
|
|
|
history_openai_format = [ |
|
{"role": "system", "content": "You are an assistant for an eCommerce store."}] |
|
for human, assistant in history: |
|
history_openai_format.append({"role": "user", "content": human}) |
|
history_openai_format.append( |
|
{"role": "assistant", "content": assistant}) |
|
history_openai_format.append({"role": "user", "content": message}) |
|
|
|
response = client.post("/query-handler/", json={"text": message, "history": history_openai_format}) |
|
if response.status_code == 200: |
|
return response.json().get("generative response") |
|
else: |
|
return "Error: Could not fetch response." |
|
|
|
|
|
iface = gr.ChatInterface( |
|
fn=fastlane_agent, |
|
chatbot=gr.Chatbot(height=400), |
|
textbox=gr.Textbox( |
|
placeholder="How can I help you?", container=False, scale=7 |
|
), |
|
title="Fastlane Chat GPT", |
|
description="AI sales assistance for e-commmerce", |
|
theme="soft", |
|
examples=["Hello", "What is the status of my order?", |
|
"Recommend me products"], |
|
cache_examples=True, |
|
retry_btn=None, |
|
undo_btn="Delete Previous", |
|
clear_btn="Clear" |
|
).launch() |
|
|
|
app = gr.mount_gradio_app(app, iface, path="./") |
|
|