File size: 4,199 Bytes
c81175d
83019fb
 
c81175d
 
83019fb
 
 
 
ccec9b3
 
 
83019fb
 
 
c81175d
 
 
 
 
 
 
 
 
 
83019fb
 
c81175d
83019fb
 
 
 
 
 
 
 
 
 
ccec9b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e104ed8
 
 
 
 
 
83019fb
 
 
 
 
ccec9b3
e104ed8
 
 
83019fb
 
 
ccec9b3
83019fb
 
 
 
 
 
 
 
ccec9b3
 
83019fb
 
88b3475
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
import gradio as gr
from constants import METRICS_TAB_TEXT
import requests
import re
import uuid
import random
import time


handshake_url = "http://127.0.0.1:8000/chatbot/"
dialog_url = "http://127.0.0.1:8000/chatbot/dialoginfo"
finish_query_url = "http://127.0.0.1:8000/chatbot/finishquery"
PRESET_ANSWERS = ["刚到美国的时候,觉得美国人像傻子一样,到处都是漏洞。任何地方的厕所都有免费纸,有些人定期去扯很多回家,纸都不用买。快餐店的饮料,有的可以无限续杯,有些几个人买一份饮料,接回来灌到各自的杯子里;等等。",
                  "尽管美国有许多“漏洞”,但作为超级大国,显然能带给人以无尽的故事与思考。我来分享一下哪些是去了美国才知道的事,主题主要围绕着生活、衣食住行、文化冲击、教育医疗等展开叙说,本文有5千字左右,你也可以跳到感兴趣的部分阅读。"
                  "美国的城市风貌与基础设施1、去到了美国才知道,纽约的城市样貌跟我想象的发达不一样,真实的纽约街景是很嘈杂和市井。例如,在曼哈顿区路旁,随处可见的小摊位,卖鲜花的、卖各种小食、卖自制首饰的,卖艺术品等等。我留意一下,发现每个路边摊都有合法的营业执照。"]


def get_internet_ip():
    r = requests.get("http://txt.go.sohu.com/ip/soip")
    ip = re.findall(r'\d+.\d+.\d+.\d+', r.text)
    if ip is not None and len(ip) > 0:
        return ip[0]
    return None


def get_chat_answer(query, chatbot):
    chatbot.append([query, ""])
    q_id = str(uuid.uuid1())
    this_answer = random.choice(PRESET_ANSWERS)
    for i in range(0, len(this_answer), 3):
        time.sleep(0.5)
        print(uuid.uuid1())
        chatbot[-1][-1] = this_answer[:i] + "▌"
        yield "", chatbot
    chatbot[-1][-1] = this_answer
    yield "", chatbot


def get_chat_answer_request(query, chatbot):
    q_id = str(uuid.uuid1())
    r = requests.post(handshake_url, json={"q_id": q_id, "query": query, 'history': chatbot})
    if r.json()['code'] == 202:
        for _ in range(1000):
            r = requests.get(dialog_url, {"dialogid": q_id})
            history = r.json()['history']
            print(history)
            while len(history[-1]) == 0:
                    history.pop()
            r_finish = requests.get(finish_query_url, {"dialogid": q_id})
            if r_finish.json() == True:
                yield "", history
                print('break')
                break

            yield "", history
            time.sleep(1)


def hello(profile: gr.OAuthProfile | None) -> str:
    if profile is None:
        return "I don't know you."
    return f"Hello {profile.name}"


css = """
col-container {max-width: 510px; margin-left: auto; margin-right: auto; margin-up: auto; margin-bottom: auto;}
"""
with gr.Blocks(css=css) as demo:
    with gr.Column(elem_id="col-container"):
        with gr.Tab("🧠 模型对话 Dialog"):
            gr.LoginButton()
            gr.LogoutButton()
            gr.Markdown().attach_load_event(hello, None)
            chatbot = gr.Chatbot([[None, get_internet_ip()]], show_label=False, elem_id="chatbot")
            query = gr.Textbox(show_label=False, placeholder="请输入提问内容,按回车进行提交")
            query.submit(get_chat_answer,[query, chatbot], [query, chatbot])
        with gr.Tab("🎚️ 模型微调 Fine-tune"):
            with gr.Row():
                with gr.Column():
                    ft_task_name = gr.Textbox(value="", label="任务名称")
                    ft_task_type = gr.Dropdown(["cat", "dog", "bird"], label="任务类型", info="要创建任务的类型")
                with gr.Column():
                    ft_epochs = gr.Textbox(value="", label="epochs")
                    ft_start_lr = gr.Textbox(value="", label="start LR")
                    ft_end_lr = gr.Textbox(value="", label="end LR")
                    submit_btn = gr.Button(value="Submit")
        with gr.Tab("📈 信息 Info"):
            gr.Markdown(METRICS_TAB_TEXT, elem_classes="markdown-text")

demo.queue(max_size=20).launch()