import gradio as gr 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() # 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 user_data = dict() live_user = dict() chat_history = [] def register(id, pw): if not id in user_data: user_data[id] = pw return "ok" else: return "fail" def login(id, pw): if not id in user_data: return "fail" else: if user_data[id] != pw: return "fail" else: live_user[id] = 100 return "ok" def chat(name, text): if not name in user_data: return "no id" else: chat_history.append({"name": name, "text":text}) return "ok" def get_data(name): for u in live_user.keys(): if u == name: live_user[u] += 20 else: live_user[u] -= 1 if live_user[u] < 0: del live_user[u] return chat_history def get_live_user(): return live_user.keys() def clear_data(): global chat_history chat_history = [] return "ok" with gr.Blocks() as demo: count = 0 aa = gr.Interface( fn=chat, inputs=["text", "text"], outputs="text", description="chat", ) bb = gr.Interface( fn=login, inputs=["text", "text"], outputs="text", description="login", ) cc = gr.Interface( fn=register, inputs=["text", "text"], outputs="text", description="register", ) dd = gr.Interface( fn=get_data, inputs=["text"], outputs="text", description="get_data", ) gg = gr.Interface( fn=get_live_user, inputs=[], outputs="text", description="get_live_user", ) ss = gr.Interface( fn=clear_data, inputs=[], outputs="text", description="clear_data", ) demo.queue(max_size=32).launch()