13ilguun commited on
Commit
769a63d
·
1 Parent(s): bcfd1f0
Files changed (1) hide show
  1. app.py +27 -24
app.py CHANGED
@@ -2,18 +2,22 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
 
5
  token = os.getenv("huggingface_token")
6
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=token)
7
 
8
- def respond(
9
- message,
10
- history: list[tuple[str, str]],
11
- system_message,
12
- max_tokens,
13
- temperature,
14
- top_p,
15
- ):
 
 
16
  messages = [{"role": "system", "content": system_message}]
 
17
  for user, assistant in history:
18
  if user:
19
  messages.append({"role": "user", "content": user})
@@ -33,10 +37,10 @@ def respond(
33
  response += token_piece
34
  yield response
35
 
36
- # Make a version of `respond` that's API-callable
37
- def chat_api(message, max_tokens, temperature, top_p):
38
- # Create a minimal prompt with default system message and no chat history
39
- messages = [{"role": "system", "content": DEFAULT_SYSTEM_MESSAGE}]
40
  messages.append({"role": "user", "content": message})
41
 
42
  output = ""
@@ -50,24 +54,23 @@ def chat_api(message, max_tokens, temperature, top_p):
50
  output += chunk.choices[0].delta.content
51
  return output
52
 
53
- DEFAULT_SYSTEM_MESSAGE = (
54
- "I am Bilguun, a third year software engineering student at McGill University and the founder of VentureCircle. "
55
- "VentureCircle is based in Montreal. "
56
- "I created this chatbot, LaunchPad, to help aspiring entrepreneurs achieve their goals. "
57
- "Your name is LaunchPad. "
58
- "LaunchPad is a friendly AI assistant designed to provide useful and supportive guidance for starting and growing a startup."
59
- )
60
-
61
- # Main demo UI
62
  demo = gr.ChatInterface(
63
  respond,
64
  additional_inputs=[
65
- gr.Textbox(value=DEFAULT_SYSTEM_MESSAGE, label="System message"),
 
 
 
 
66
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
67
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
68
  gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
69
  ],
70
  )
71
 
72
- # Launch with /chat endpoint
73
- demo.launch(share=True, show_error=True).api(fn=chat_api, inputs=["text", "slider", "slider", "slider"], api_name="/chat")
 
 
 
 
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
+ # Load token and create client
6
  token = os.getenv("huggingface_token")
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=token)
8
 
9
+ # Load system messages from environment
10
+ SYSTEM_MESSAGES = {
11
+ "LaunchPad": os.getenv("DEFAULT_SYSTEM_MESSAGE_1", "Default LaunchPad message."),
12
+ "MentorBot": os.getenv("DEFAULT_SYSTEM_MESSAGE_2", "Default MentorBot message."),
13
+ "CoachAI": os.getenv("DEFAULT_SYSTEM_MESSAGE_3", "Default CoachAI message."),
14
+ }
15
+
16
+ # Chat function used by Gradio UI
17
+ def respond(message, history, persona, max_tokens, temperature, top_p):
18
+ system_message = SYSTEM_MESSAGES.get(persona, "Default fallback message.")
19
  messages = [{"role": "system", "content": system_message}]
20
+
21
  for user, assistant in history:
22
  if user:
23
  messages.append({"role": "user", "content": user})
 
37
  response += token_piece
38
  yield response
39
 
40
+ # API-callable version
41
+ def chat_api(message, persona, max_tokens, temperature, top_p):
42
+ system_message = SYSTEM_MESSAGES.get(persona, "Default fallback message.")
43
+ messages = [{"role": "system", "content": system_message}]
44
  messages.append({"role": "user", "content": message})
45
 
46
  output = ""
 
54
  output += chunk.choices[0].delta.content
55
  return output
56
 
57
+ # Gradio UI
 
 
 
 
 
 
 
 
58
  demo = gr.ChatInterface(
59
  respond,
60
  additional_inputs=[
61
+ gr.Dropdown(
62
+ choices=list(SYSTEM_MESSAGES.keys()),
63
+ value="LaunchPad",
64
+ label="Select Persona"
65
+ ),
66
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
67
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
68
  gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
69
  ],
70
  )
71
 
72
+ # Serve UI and API
73
+ demo.launch(share=True, show_error=True)
74
+
75
+ # To add an API endpoint, you must use FastAPI + mount if needed.
76
+ # Ask if you'd like that added again.