File size: 2,747 Bytes
01d1dd6
c2f1466
722ab73
c2f1466
 
 
cf7a07e
5aca742
b0d9242
 
 
a7feab8
7192ffe
da9b438
f718849
c2f1466
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5aca742
 
628eb7c
 
4d210d2
 
 
 
 
 
e3904de
4d210d2
 
 
 
 
 
 
 
 
 
628eb7c
4d210d2
 
e3904de
7a95186
 
 
 
31e1a3e
9ddf3dd
 
 
 
8f1da7f
5cf95a1
9ddf3dd
5cf95a1
a7feab8
628eb7c
4d210d2
628eb7c
 
 
 
5aca742
0e70681
f348156
5aca742
0e70681
628eb7c
 
c2f1466
5be15aa
d79f686
5be15aa
36a4af1
628eb7c
5be15aa
6872135
1979413
ddcca98
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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, queue
import math
import time
import datetime
import requests, json

loop = asyncio.get_event_loop()
gradio_client = GrClient(os.environ.get('GrClient_url'), serialize=False)
# 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


q = queue.Queue()

def chat():
    while True:
        time.sleep(1)
        global q
        if not q.empty():
            arr = q.get()
            start = time.time()
            print("응닡 μ‹œμž‘\nx:"+ arr[0] +"\nid:"+ arr[1] +"\ncdata:" + arr[2] + "\ncollback_url : " + arr[3])
            result = gradio_client.predict(
                arr[0],
                # str representing input in 'User input' Textbox component
                arr[1],
                arr[2],
        		fn_index=0
            )
            
            end = time.time()
        
        
            sec = (end - start)
            result_list = str(datetime.timedelta(seconds=sec)).split(".")
            print("응닡 μ‹œκ°„ : " + result_list[0] +"\nresult:"+ result)

            if result.split(":")[0]=="상담원":
                result = result.split(":")[1]

            headers = {'Content-Type': 'application/json'}
            body = {
                "url" : arr[3],
                "data" : result
            }
            
            if arr[3] != "Asia/Seoul":
                response_collback = requests.post("https://9174963504.for-seoul.synctreengine.com/call", headers=headers, data=json.dumps(body))
                print(response_collback)
        
th_a = Thread(target = chat)
th_a.daemon = True
th_a.start()

# App code
def res(x, id, cdata, url):    
    global q

    arr = [x, id, str(cdata.split(",", 1)[0]), url]
    q.put(arr)

    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)