AfricaComputeFund commited on
Commit
bcb79a9
·
verified ·
1 Parent(s): 17d57dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -12
app.py CHANGED
@@ -3,17 +3,19 @@ import requests
3
 
4
  API_URL = "https://oocsd96vwjv6tw6a.us-east-1.aws.endpoints.huggingface.cloud"
5
 
6
- def query(message, history, system_message, temperature, top_p):
7
  if history is None:
8
  history = []
9
  full_prompt = f"{system_message}\n\n"
10
- # Handle both new messages format and legacy tuple format
11
  if history and isinstance(history[0], dict):
12
  for entry in history:
13
- if entry.get("role") == "user":
14
- full_prompt += f"User: {entry.get('content')}\n"
15
- elif entry.get("role") == "assistant":
16
- full_prompt += f"Assistant: {entry.get('content')}\n"
 
 
17
  elif history and isinstance(history, list):
18
  for pair in history:
19
  if isinstance(pair, (list, tuple)) and len(pair) == 2:
@@ -21,15 +23,20 @@ def query(message, history, system_message, temperature, top_p):
21
  full_prompt += f"User: {user_msg}\nAssistant: {assistant_msg}\n"
22
  full_prompt += f"User: {message}\nAssistant:"
23
 
 
 
 
24
  payload = {
25
  "inputs": full_prompt,
26
  "parameters": {
27
  "temperature": temperature,
28
  "top_p": top_p,
29
- "max_new_tokens": 512,
30
- "stop": ["User:", "Assistant:"]
31
  }
32
  }
 
 
 
33
  headers = {"Content-Type": "application/json"}
34
  response = requests.post(f"{API_URL}/generate", json=payload, headers=headers)
35
  result = response.json()
@@ -45,14 +52,16 @@ demo = gr.ChatInterface(
45
  title="Monarch-1 Chatbot",
46
  description="Chat directly with Monarch-1, a generative AI optimized for African contexts.",
47
  examples=[
48
- ["Tell me about ancient African civilizations.", None, "You are Monarch-1, an AI model optimized for African contexts.", 0.7, 0.9],
49
- ["Explain economic trends in Africa.", None, "You are Monarch-1, an AI model optimized for African contexts.", 0.7, 0.9],
50
- ["Teach me some Kiswahili phrases.", None, "You are Monarch-1, an AI model optimized for African contexts.", 0.7, 0.9]
51
  ],
52
  additional_inputs=[
53
  gr.Textbox(value="You are Monarch-1, an AI model optimized for African contexts.", label="System message"),
54
  gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature"),
55
- gr.Slider(minimum=0.5, maximum=1.0, value=0.9, step=0.05, label="Top-p (nucleus sampling)")
 
 
56
  ],
57
  type="messages"
58
  )
 
3
 
4
  API_URL = "https://oocsd96vwjv6tw6a.us-east-1.aws.endpoints.huggingface.cloud"
5
 
6
+ def query(message, history, system_message, temperature, top_p, max_new_tokens, stop_tokens):
7
  if history is None:
8
  history = []
9
  full_prompt = f"{system_message}\n\n"
10
+ # Process conversation history, supporting both new and legacy formats.
11
  if history and isinstance(history[0], dict):
12
  for entry in history:
13
+ role = entry.get("role")
14
+ content = entry.get("content")
15
+ if role == "user":
16
+ full_prompt += f"User: {content}\n"
17
+ elif role == "assistant":
18
+ full_prompt += f"Assistant: {content}\n"
19
  elif history and isinstance(history, list):
20
  for pair in history:
21
  if isinstance(pair, (list, tuple)) and len(pair) == 2:
 
23
  full_prompt += f"User: {user_msg}\nAssistant: {assistant_msg}\n"
24
  full_prompt += f"User: {message}\nAssistant:"
25
 
26
+ # Process stop tokens input into a list if provided.
27
+ stop_list = [s.strip() for s in stop_tokens.split(",")] if stop_tokens.strip() != "" else None
28
+
29
  payload = {
30
  "inputs": full_prompt,
31
  "parameters": {
32
  "temperature": temperature,
33
  "top_p": top_p,
34
+ "max_new_tokens": max_new_tokens,
 
35
  }
36
  }
37
+ if stop_list is not None:
38
+ payload["parameters"]["stop"] = stop_list
39
+
40
  headers = {"Content-Type": "application/json"}
41
  response = requests.post(f"{API_URL}/generate", json=payload, headers=headers)
42
  result = response.json()
 
52
  title="Monarch-1 Chatbot",
53
  description="Chat directly with Monarch-1, a generative AI optimized for African contexts.",
54
  examples=[
55
+ ["Tell me about ancient African civilizations.", "You are Monarch-1, an AI model optimized for African contexts.", 0.7, 0.9, 512, "User:,Assistant:"],
56
+ ["Explain economic trends in Africa.", "You are Monarch-1, an AI model optimized for African contexts.", 0.7, 0.9, 512, "User:,Assistant:"],
57
+ ["Teach me some Kiswahili phrases.", "You are Monarch-1, an AI model optimized for African contexts.", 0.7, 0.9, 512, "User:,Assistant:"]
58
  ],
59
  additional_inputs=[
60
  gr.Textbox(value="You are Monarch-1, an AI model optimized for African contexts.", label="System message"),
61
  gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature"),
62
+ gr.Slider(minimum=0.5, maximum=1.0, value=0.9, step=0.05, label="Top-p (nucleus sampling)"),
63
+ gr.Slider(minimum=10, maximum=1024, value=512, step=10, label="Max New Tokens"),
64
+ gr.Textbox(value="User:,Assistant:", label="Stop Tokens (comma-separated)")
65
  ],
66
  type="messages"
67
  )