api_for_chat / app.py
ldhldh's picture
Update app.py
36a4af1
raw
history blame
2.2 kB
from threading import Thread
import gradio as gr
from gradio_client import Client as GrClient
import inspect
from gradio import routes
from typing import List, Type
import requests, os, re, asyncio
import math
import time
import datetime
loop = asyncio.get_event_loop()
gradio_client = GrClient(os.environ.get('GrClient_url'))
# Monkey patch
def get_types(cls_set: List[Type], component: str):
docset = []
types = []
if component == "input":
for cls in cls_set:
doc = inspect.getdoc(cls)
doc_lines = doc.split("\n")
docset.append(doc_lines[1].split(":")[-1])
types.append(doc_lines[1].split(")")[0].split("(")[-1])
else:
for cls in cls_set:
doc = inspect.getdoc(cls)
doc_lines = doc.split("\n")
docset.append(doc_lines[-1].split(":")[-1])
types.append(doc_lines[-1].split(")")[0].split("(")[-1])
return docset, types
routes.get_types = get_types
_x = ""
_id = ""
_cdata = ""
_url = ""
_do = False
def chat():
global _do
global _x
global _id
global _cdata
if _do:
_do = False
start = time.time()
result = gradio_client.predict(
_x,
# str representing input in 'User input' Textbox component
_id,
_cdata,
fn_index=0
)
result = str(result)
end = time.time()
sec = (end - start)
result_list = str(datetime.timedelta(seconds=sec)).split(".")
print("응닡 μ‹œκ°„ : " + result_list[0] +"\nx:"+ x +"\nid:"+ id +"\ncdata:" + cdata +"\nresult:"+ result)
return result
th_a = Thread(target = chat)
th_a.daemon = True
th_a.start()
# App code
def res(x, id, cdata, url):
global _do
global _x
global _id
global _cdata
global _url
_x = x
_id = id
_cdata = cdata
_url = url
_do = True
print("\n_Done\n\n")
return "Done"
with gr.Blocks() as demo:
count = 0
aa = gr.Interface(
fn=res,
inputs=["text","text", "text", "text"],
outputs="text",
description="chat",
)
demo.queue(max_size=32).launch(enable_queue=True)