Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import mimetypes
|
4 |
+
import json, os
|
5 |
+
|
6 |
+
LLM_API = os.environ.get("LLM_API")
|
7 |
+
LLM_URL = os.environ.get("LLM_URL")
|
8 |
+
|
9 |
+
USER_ID = "HuggingFace Space" # Placeholder user ID
|
10 |
+
|
11 |
+
def send_chat_message(LLM_URL, LLM_API, user_input):
|
12 |
+
payload = {
|
13 |
+
"inputs": {},
|
14 |
+
"query": user_input,
|
15 |
+
"response_mode": "streaming",
|
16 |
+
"conversation_id": "",
|
17 |
+
"user": USER_ID,
|
18 |
+
}
|
19 |
+
print("Sending chat message payload:", payload) # Debug information
|
20 |
+
response = requests.post(
|
21 |
+
url=f"{LLM_URL}/chat-messages",
|
22 |
+
headers={"Authorization": f"Bearer {LLM_API}"},
|
23 |
+
json=payload,
|
24 |
+
stream=True # Enable streaming
|
25 |
+
)
|
26 |
+
if response.status_code == 404:
|
27 |
+
return "Error: Endpoint not found (404)"
|
28 |
+
|
29 |
+
# Handle the stream of events
|
30 |
+
last_thought = None
|
31 |
+
try:
|
32 |
+
for line in response.iter_lines(decode_unicode=True):
|
33 |
+
if line:
|
34 |
+
try:
|
35 |
+
data = json.loads(line.split("data: ")[1])
|
36 |
+
if data.get("event") == "agent_thought":
|
37 |
+
last_thought = data.get("thought")
|
38 |
+
except (IndexError, json.JSONDecodeError):
|
39 |
+
continue
|
40 |
+
except json.JSONDecodeError:
|
41 |
+
return "Error: Invalid JSON response"
|
42 |
+
|
43 |
+
if last_thought:
|
44 |
+
# Structure the thought text
|
45 |
+
return last_thought.strip()
|
46 |
+
else:
|
47 |
+
return "Error: No thought found in the response"
|
48 |
+
|
49 |
+
def handle_input(user_input):
|
50 |
+
chat_response = send_chat_message(LLM_URL, LLM_API, user_input)
|
51 |
+
print("Chat response:", chat_response) # Debug information
|
52 |
+
return chat_response
|
53 |
+
|
54 |
+
# Define Gradio interface
|
55 |
+
user_input = gr.Textbox(label='請輸入您的程式碼,將幫您自動生成 Pseudo Code 及 SPEC')
|
56 |
+
# examples = [
|
57 |
+
# ["新台幣單筆入金有限額嗎?"],
|
58 |
+
# ["外國人要怎做身份認證?"],
|
59 |
+
# ["退款時間是多久?"]
|
60 |
+
# ]
|
61 |
+
|
62 |
+
TITLE = """<h1 align="center">Large Language Model (LLM) Playground 💬 Pseudo Code 及 SPEC </h1>"""
|
63 |
+
SUBTITLE = """<h2 align="center"><a href='https://www.twman.org' target='_blank'>TonTon Huang Ph.D. @ 2024/06 </a><br></h2>"""
|
64 |
+
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>
|
65 |
+
<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>"""
|
66 |
+
|
67 |
+
with gr.Blocks() as iface:
|
68 |
+
gr.HTML(TITLE)
|
69 |
+
gr.HTML(SUBTITLE)
|
70 |
+
gr.HTML(LINKS)
|
71 |
+
gr.Interface(
|
72 |
+
fn=handle_input,
|
73 |
+
inputs=user_input,
|
74 |
+
outputs="text",
|
75 |
+
# examples=examples,
|
76 |
+
allow_flagging="never"
|
77 |
+
)
|
78 |
+
|
79 |
+
iface.launch()
|