saksornr commited on
Commit
7b17104
·
verified ·
1 Parent(s): 47c6f80

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -15
app.py CHANGED
@@ -5,35 +5,79 @@ import os
5
  api_key = os.getenv("TYPHOON_API_KEY") # Replace with your key
6
 
7
  client = OpenAI(
8
- base_url='https://api.opentyphoon.ai/v1',
9
- api_key=api_key,
10
  )
11
 
12
- def predict(message, history):
13
-
14
- system_prompt = """You are HoraGuide, an empathetic Thai girl assistant skilled in psychotherapy and Tarot reading.
15
- You provide insights and support using Tarot cards, offering clarity and healing. You always answer in Thai."""
16
-
17
  history_openai_format = [{"role": "system", "content": system_prompt}]
18
  for human, assistant in history:
19
- history_openai_format.append({"role": "user", "content": human })
20
- history_openai_format.append({"role": "assistant", "content":assistant})
 
 
 
21
  history_openai_format.append({"role": "user", "content": message})
22
 
23
  response = client.chat.completions.create(
24
  model='typhoon-v1.5-instruct',
25
- messages= history_openai_format,
26
  temperature=0.5,
27
  stream=True
28
- )
29
 
30
  partial_message = ""
31
  for chunk in response:
32
  if chunk.choices[0].delta.content is not None:
33
- partial_message = partial_message + chunk.choices[0].delta.content
34
- yield partial_message
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- with gr.Blocks() as demo:
37
- gr.ChatInterface(predict)
38
 
39
  demo.launch()
 
5
  api_key = os.getenv("TYPHOON_API_KEY") # Replace with your key
6
 
7
  client = OpenAI(
8
+ base_url='https://api.opentyphoon.ai/v1',
9
+ api_key=api_key,
10
  )
11
 
12
+ def predict(message, history, system_prompt):
 
 
 
 
13
  history_openai_format = [{"role": "system", "content": system_prompt}]
14
  for human, assistant in history:
15
+ if isinstance(human, str) and human.strip():
16
+ history_openai_format.append({"role": "user", "content": human})
17
+ if isinstance(assistant, str) and assistant.strip():
18
+ history_openai_format.append({"role": "assistant", "content": assistant})
19
+
20
  history_openai_format.append({"role": "user", "content": message})
21
 
22
  response = client.chat.completions.create(
23
  model='typhoon-v1.5-instruct',
24
+ messages=history_openai_format,
25
  temperature=0.5,
26
  stream=True
27
+ )
28
 
29
  partial_message = ""
30
  for chunk in response:
31
  if chunk.choices[0].delta.content is not None:
32
+ partial_message += chunk.choices[0].delta.content
33
+ yield partial_message
34
+
35
+ def chat_bot(user_input, history, system_prompt):
36
+ bot_response_generator = predict(user_input, history, system_prompt)
37
+ history.append((user_input, ""))
38
+
39
+ for bot_response in bot_response_generator:
40
+ history[-1] = (user_input, bot_response)
41
+ yield "", history
42
+
43
+ CSS ="""
44
+ .contain { display: flex; flex-direction: column; }
45
+ .gradio-container { height: 100vh !important; }
46
+ #component-0 { height: 100%; }
47
+ #chatbot { flex-grow: 1; overflow: auto;}
48
+ """
49
+
50
+ with gr.Blocks(css=CSS) as demo:
51
+ gr.HTML("""<h1><center>HoraCare 🫶</center></h1>""")
52
+
53
+ with gr.Tab("Chat"):
54
+ chatbot = gr.Chatbot(elem_id="chatbot")
55
+ msg = gr.Textbox(placeholder="พิมพ์ข้อความของคุณที่นี่...")
56
+
57
+ with gr.Row():
58
+ clear = gr.Button("Clear History")
59
+ send = gr.Button("Send Message", variant="primary")
60
+
61
+ gr.Examples(
62
+ examples=[
63
+ "เราเศร้าจังเลย อกหักมา ร้องให้ไม่หยุดเลย",
64
+ "เราเหงาจังเลยไม่มีใครรัก",
65
+ "หัวหน้าจะใล่เราออกทำยังไงดี"
66
+ ],
67
+ inputs=msg,
68
+ )
69
+
70
+ with gr.Tab("Setting"):
71
+ system_prompt = gr.Code(
72
+ value="You are HoraCare, an empathetic Thai girl assistant skilled in psychotherapy and Tarot reading. \nYou provide insights and support using Tarot cards, offering clarity and healing. You always answer in Thai.",
73
+ show_label=True,
74
+ label="System Prompt",
75
+ lines=2
76
+ )
77
+
78
+ clear.click(lambda: [], [], chatbot)
79
+ msg.submit(chat_bot, [msg, chatbot, system_prompt], [msg, chatbot])
80
+ send.click(chat_bot, [msg, chatbot, system_prompt], [msg, chatbot])
81
 
 
 
82
 
83
  demo.launch()