File size: 1,872 Bytes
106132f
 
 
 
 
 
 
 
 
 
0564d5c
106132f
e627654
 
e44d315
 
 
 
 
 
 
106132f
 
 
 
22e7460
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106132f
 
 
 
 
 
 
 
 
22e7460
106132f
22e7460
106132f
 
 
1
2
3
4
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from transformers import pipeline
import gradio as gr
from fastapi import FastAPI
from gradio_client import Client
from typing import Union
import uvicorn


app = FastAPI()

chatbot = pipeline(model="Kaludi/Customer-Support-Assistant-V2")

def print_like_dislike(x: gr.LikeData):
    print(x.index, x.value, x.liked)
def add_message(history, message):
    for x in message["files"]:
        history.append(((x,), None))
    if message["text"] is not None:
        history.append((message["text"], None))
    return history, gr.MultimodalTextbox(value=None, interactive=False)

def DS_chatbot(message,history):
    conversation = chatbot(message)
    return conversation[0]['generated_text']

'''io = gr.ChatInterface(DS_chatbot, title=" Customer Service Bot", description="Enter your query.")'''
with gr.Blocks() as demo:
    chat_bot = gr.Chatbot(
        [],
        elem_id="Customer Service Bot",
        bubble_full_width=False,
    )

    chat_input = gr.MultimodalTextbox(interactive=True, file_types=["image"], placeholder="Enter message or upload file...", show_label=False)

    chat_msg = chat_input.submit(add_message, [chat_bot, chat_input], [chat_bot, chat_input])
    bot_msg = chat_msg.then(DS_chatbot, chat_bot, chat_bot, api_name="bot_response")
    bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])

    chat_bot.like(print_like_dislike, None, None)
    
@app.get("/")
async def read_main():
    return {"message": "Append /gradio to the url to see gradio the interface"
           ,"message2": "Append /hello/{any_name} to get a greeting"}

@app.get("/hello/{name}")
async def read_name(name: Union[str, None] = None):
    return { "Hey!": name}


# Mount the Gradio app onto the FastAPI app
app = gr.mount_gradio_app(app, demo, path='/gradio')
if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)