Realstate / app.py
amirshafiei's picture
Add app file
267bd18
raw
history blame
3.03 kB
import time
import gradio as gr
from random import uniform
from state_handler import state_handler
# with gr.Blocks(css=".gradio-container {text-align: right;}") as demo:
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.ClearButton([msg, chatbot])
user_state = gr.State(value=None)
user_info = gr.State(value=None)
gathered_info = gr.State(value=None)
def user(user_message, history):
return gr.update(value=user_message, interactive=True), history + [[user_message, None]]
def respond(message, chat_history, user_state, user_info,gathered_info):
# user_state, bot_message, user_info = state_handler(user_state, message, user_info)
user_state,user_info,gathered_info = state_handler(message,user_state, user_info,gathered_info)
bot_message = user_info
time_sleep = uniform(0.004, 0.005) if len(bot_message) < 80 else uniform(0.005, 0.006)
chat_history[-1][1] = ""
for character in bot_message:
chat_history[-1][1] += character
time.sleep(time_sleep)
yield "", chat_history, user_state, user_info, gathered_info
msg.submit(
user,
[msg, chatbot],
[msg, chatbot]
).then(
respond,
[msg, chatbot, user_state, user_info, gathered_info],
[msg, chatbot, user_state, user_info, gathered_info]
).then(
lambda: gr.update(interactive=True),
None,
[msg],
queue=False
)
demo.queue()
# demo.launch() #
demo.launch() #
# demo.launch(server_name='0.0.0.0', server_port=8555,share=True) #
# import gradio as gr
# import requests
# import json
# with gr.Blocks(css=".gradio-container {text-align: right;}") as demo:
# chatbot = gr.Chatbot()
# msg = gr.Textbox()
# clear = gr.ClearButton([msg, chatbot])
# user_state = gr.State(value=0)
# def send_request(msg):
# url = "http://localhost:5005/webhooks/rest/webhook"
# # url = "http://192.168.1.170:9901/webhooks/rest/webhook"
# payload = json.dumps({
# "sender": "test_user",
# "message": msg,
# "platform_type": "telegram",
# "metadata": {}
# })
# headers = {
# 'Content-Type': 'application/json'
# }
# return requests.request("POST", url, headers=headers, data=payload)
# def respond(message, chat_history):
# responses = send_request(message)
# if responses.status_code != 200:
# return "خطا در اتصال"
# responses = responses.json()
# bot_message = "\n\n".join([response['text'] for response in responses])
# # for response in responses:
# # bot_message = response['text']
# chat_history.append((message, bot_message))
# return "", chat_history
# msg.submit(respond, [msg, chatbot], [msg, chatbot])
# demo.launch(
# server_name="0.0.0.0",
# server_port=8000,
# )