DeepLearning101 commited on
Commit
10b839f
·
verified ·
1 Parent(s): fb8a99d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -55
app.py CHANGED
@@ -1,64 +1,89 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
 
26
- messages.append({"role": "user", "content": message})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
 
 
 
 
 
 
 
 
 
 
 
 
42
 
 
 
 
 
 
 
 
 
 
 
43
  """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
 
63
- if __name__ == "__main__":
64
- demo.launch()
 
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='請輸入您想查詢的關鍵公司名稱')
56
+ examples = [
57
+ ["加密貨幣"],
58
+ ["國泰金控"],
59
+ ["中華電信"],
60
+ ["台灣大哥大"],
61
+ ["台積電"],
62
+ ["BlockTempo"],
63
+ ["abmedia"]
64
+ ]
65
 
66
+ TITLE = """<h1>Social Media Trends 💬 分析社群相關資訊,並判斷其正、負、中立等評價及趨勢 </h1>"""
67
+ SUBTITLE = """<h2><a href='https://www.twman.org' target='_blank'>TonTon Huang Ph.D. @ 2024/11 </a><br></h2>"""
68
+ LINKS = """
69
+ <a href='https://reurl.cc/g6GlZX' target='_blank'>手把手帶你一起踩AI坑</a><br>
70
+ <a href='https://blog.twman.org/2024/11/diffusion.html' target='_blank'>ComfyUI + Stable Diffuision</a><br>
71
+ <a href='https://blog.twman.org/2024/08/LLM.html' target='_blank'>白話文手把手帶你科普 GenAI</a> | <a href='https://blog.twman.org/2024/09/LLM.html' target='_blank'>大型語言模型直接就打完收工?</a><br>
72
+ <a href='https://blog.twman.org/2023/04/GPT.html' target='_blank'>什麼是大語言模型,它是什麼?想要嗎?</a> | <a href='https://blog.twman.org/2024/07/RAG.html' target='_blank'>那些檢索增強生成要踩的坑 </a><br>
73
+ <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><br>
74
+ <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><br>
75
+ <a href='https://blog.twman.org/2023/07/wsl.html' target='_blank'>用PPOCRLabel來幫PaddleOCR做OCR的微調和標註</a> | <a href='https://blog.twman.org/2023/07/HugIE.html' target='_blank'>基於機器閱讀理解和指令微調的統一信息抽取框架之診斷書醫囑資訊擷取分析</a><br>
76
  """
77
+ with gr.Blocks() as iface:
78
+ gr.HTML(TITLE)
79
+ gr.HTML(SUBTITLE)
80
+ gr.HTML(LINKS)
81
+ gr.Interface(
82
+ fn=handle_input,
83
+ inputs=user_input,
84
+ outputs="text",
85
+ # examples=examples,
86
+ allow_flagging="never"
87
+ )
 
 
 
 
 
 
 
88
 
89
+ iface.launch()