DeepLearning101's picture
Update app.py
1964be5 verified
raw
history blame
3.44 kB
import gradio as gr
import requests
import mimetypes
import json, os
LLM_API = os.environ.get("LLM_API")
LLM_URL = os.environ.get("LLM_URL")
USER_ID = "HuggingFace Space" # Placeholder user ID
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
response = requests.post(
url=f"{LLM_URL}/chat-messages",
headers={"Authorization": f"Bearer {LLM_API}"},
json=payload,
stream=True # Enable streaming
)
if response.status_code == 404:
return "Error: Endpoint not found (404)"
# Handle the stream of events
last_thought = None
try:
for line in response.iter_lines(decode_unicode=True):
if line:
try:
data = json.loads(line.split("data: ")[1])
if data.get("event") == "agent_thought":
last_thought = data.get("thought")
except (IndexError, json.JSONDecodeError):
continue
except json.JSONDecodeError:
return "Error: Invalid JSON response"
if last_thought:
# Structure the thought text
return last_thought.strip()
else:
return "Error: No thought found in the response"
def handle_input(user_input):
chat_response = send_chat_message(LLM_URL, LLM_API, user_input)
print("Chat response:", chat_response) # Debug information
return chat_response
# Define Gradio interface
user_input = gr.Textbox(label='歡迎問我加密貨幣交易所的各種疑難雜症')
examples = [
["MAX 帳號刪除關戶後,又重新註冊 MAX 後要怎辦?"],
["開通約定帳號"],
["客服專線?"],
["客服信箱?"]
]
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)
gr.Interface(
fn=handle_input,
inputs=user_input,
outputs="text",
examples=examples,
allow_flagging="never"
)
iface.launch()