File size: 6,116 Bytes
79f3857
83bfed1
2eff9bd
 
79f3857
 
83bfed1
 
 
 
 
 
79f3857
2eff9bd
83bfed1
 
 
 
 
 
 
 
583bd83
2eff9bd
79f3857
 
 
 
 
 
 
 
 
 
2eff9bd
79f3857
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83bfed1
79f3857
 
 
 
 
 
 
2eff9bd
 
79f3857
2eff9bd
83bfed1
 
 
2eff9bd
79f3857
2eff9bd
 
c9c08b8
 
 
 
 
 
 
 
 
 
 
 
 
79f3857
 
83bfed1
1964be5
0f09753
5936530
f33309a
83bfed1
 
1964be5
83bfed1
 
 
 
 
 
 
 
79f3857
 
 
 
 
 
c9c08b8
 
 
 
79f3857
 
 
 
 
 
 
 
c9c08b8
 
 
 
 
79f3857
83bfed1
c9c08b8
 
 
 
 
 
 
 
 
 
 
 
583bd83
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import os
import gradio as gr
import aiohttp
import asyncio
import json
from functools import lru_cache

LLM_API = os.environ.get("LLM_API")
LLM_URL = os.environ.get("LLM_URL")

USER_ID = "HuggingFace Space"  # Placeholder user ID

@lru_cache(maxsize=32)
async def send_chat_message(LLM_URL, LLM_API, user_input):
    payload = {
        "inputs": {},
        "query": user_input,
        "response_mode": "streaming",
        "conversation_id": "",
        "user": USER_ID,
    }
    print("Sending chat message payload:", payload)  # Debug information

    async with aiohttp.ClientSession() as session:
        try:
            async with session.post(
                url=f"{LLM_URL}/chat-messages",
                headers={"Authorization": f"Bearer {LLM_API}"},
                json=payload,
                timeout=aiohttp.ClientTimeout(total=60)
            ) as response:
                if response.status != 200:
                    print(f"Error: {response.status}")
                    return f"Error: {response.status}"

                full_response = []
                async for line in response.content:
                    line = line.decode('utf-8').strip()
                    if not line:
                        continue
                    if "data: " not in line:
                        continue
                    try:
                        print("Received line:", line)  # Debug information
                        data = json.loads(line.split("data: ")[1])
                        if "answer" in data:
                            full_response.append(data["answer"])
                    except (IndexError, json.JSONDecodeError) as e:
                        print(f"Error parsing line: {line}, error: {e}")  # Debug information
                        continue

                if full_response:
                    return ''.join(full_response).strip()
                else:
                    return "Error: No response found in the response"
        except Exception as e:
            print(f"Exception: {e}")
            return f"Exception: {e}"

async def handle_input(user_input):
    print(f"Handling input: {user_input}")
    chat_response = await send_chat_message(LLM_URL, LLM_API, user_input)
    print("Chat response:", chat_response)  # Debug information
    return chat_response

def run_sync(user_input):
    print(f"Running sync with input: {user_input}")
    return asyncio.run(handle_input(user_input))

# 定義反饋處理函數
def save_feedback(user_input, response, feedback_type, improvement):
    feedback = {
        "user_input": user_input,
        "response": response,
        "feedback_type": feedback_type,
        "improvement": improvement
    }
    print(f"Saving feedback: {feedback}")
    # 假設你有一個保存反饋的機制,可以是保存到文件或發送到服務器
    # 這裡簡單打印出來,實際應用中應該保存反饋
    return "感謝您的反饋!"

# 定義 Gradio 界面
user_input = gr.Textbox(label='歡迎問我加密貨幣交易所的各種疑難雜症', placeholder='在此輸入問題...')
examples = [
    ["MAX 帳號刪除關戶後,又重新註冊 MAX 後要怎辦?"],
    ["手機APP怎麼操作掛單交易?"],
    ["USDT 怎樣換新台幣?"],
    ["新台幣入金要怎操作"]
]

TITLE = """<h1 align="center">Large Language Model (LLM) Playground 💬 <a href='https://support.maicoin.com/zh-TW/support/home' target='_blank'>Cryptocurrency Exchange FAQ</a></h1>"""
SUBTITLE = """<h2 align="center"><a href='https://www.twman.org' target='_blank'>TonTon Huang Ph.D. @ 2024/06 </a><br></h2>"""
LINKS = """<a href='https://blog.twman.org/2021/04/ASR.html' target='_blank'>那些語音處理 (Speech Processing) 踩的坑</a> | <a href='https://blog.twman.org/2021/04/NLP.html' target='_blank'>那些自然語言處理 (Natural Language Processing, NLP) 踩的坑</a> | <a href='https://blog.twman.org/2024/02/asr-tts.html' target='_blank'>那些ASR和TTS可能會踩的坑</a> | <a href='https://blog.twman.org/2024/02/LLM.html' target='_blank'>那些大模型開發會踩的坑</a> | <a href='https://blog.twman.org/2023/04/GPT.html' target='_blank'>什麼是大語言模型,它是什麼?想要嗎?</a><br>
<a href='https://blog.twman.org/2023/07/wsl.html' target='_blank'>用PaddleOCR的PPOCRLabel來微調醫療診斷書和收據</a> | <a href='https://blog.twman.org/2023/07/HugIE.html' target='_blank'>基於機器閱讀理解和指令微調的統一信息抽取框架之診斷書醫囑資訊擷取分析</a><br>"""

with gr.Blocks() as iface:
    gr.HTML(TITLE)
    gr.HTML(SUBTITLE)
    gr.HTML(LINKS)
    with gr.Row():
        chatbot = gr.Chatbot()
    with gr.Row():
        user_input = gr.Textbox(label='輸入您的問題', placeholder="在此輸入問題...")
        submit_button = gr.Button("送出")
    gr.Examples(examples=examples, inputs=user_input)
    with gr.Row():
        like_button = gr.Button("👍")
        dislike_button = gr.Button("👎")
        improvement_input = gr.Textbox(label='請輸入改進建議', placeholder='請輸入如何改進模型回應的建議')
    with gr.Row():
        feedback_output = gr.Textbox(label='反饋結果', interactive=False)

    def chat(user_input, history):
        response = run_sync(user_input)
        history.append((user_input, response))
        return history, history

    def handle_feedback(response, feedback_type, improvement):
        global last_user_input
        feedback_message = save_feedback(last_user_input, response, feedback_type, improvement)
        return feedback_message

    submit_button.click(fn=chat, inputs=[user_input, chatbot], outputs=[chatbot, chatbot])

    like_button.click(
        fn=lambda response, improvement: handle_feedback(response, "like", improvement),
        inputs=[chatbot, improvement_input],
        outputs=feedback_output
    )

    dislike_button.click(
        fn=lambda response, improvement: handle_feedback(response, "dislike", improvement),
        inputs=[chatbot, improvement_input],
        outputs=feedback_output
    )

iface.launch()