rufatronics commited on
Commit
c4c0df1
·
verified ·
1 Parent(s): a416e03

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -18
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- # Your tokens (replace with real ones)
5
  HF_TOKENS = [
6
  "hf_token_1",
7
  "hf_token_2",
@@ -35,30 +35,29 @@ def chat_with_magana(user_input, history=None):
35
  global client
36
  if history is None:
37
  history = []
 
 
 
 
 
 
 
38
  for attempt in range(len(HF_TOKENS)):
39
  try:
40
- completion = client.chat_completion(
41
- model=MODEL,
42
- messages=[
43
- {
44
- "role": "system",
45
- "content": (
46
- "You are Magana AI, an intelligent Hausa-first AI assistant. "
47
- "Always greet in Hausa first, then continue in Hausa or English depending on user preference. "
48
- "Be clear, simple, friendly. Respect Hausa culture with proverbs/greetings. "
49
- "If you don’t know something, say so honestly. Default in Hausa unless asked for English. "
50
- "Origin: Developed by Ahmad Garba Adamu from Kano."
51
- )
52
- },
53
- {"role": "user", "content": user_input}
54
- ]
55
  )
56
- reply = completion["choices"][0]["message"]["content"]
57
  history.append((user_input, reply))
58
  return history, reply
59
  except Exception as e:
60
  print("⚠️ Error:", e)
61
  switch_token()
 
62
  return history, "❌ All tokens failed."
63
 
64
  # ------------------------------
@@ -70,7 +69,7 @@ init_client(0)
70
  # Gradio UI
71
  # ------------------------------
72
  with gr.Blocks() as demo:
73
- chatbot = gr.Chatbot()
74
  msg = gr.Textbox(placeholder="Type your message here...")
75
  clear = gr.Button("Clear")
76
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ # Your model + tokens
5
  HF_TOKENS = [
6
  "hf_token_1",
7
  "hf_token_2",
 
35
  global client
36
  if history is None:
37
  history = []
38
+
39
+ # Convert history into a conversation string
40
+ conversation = "System: You are Magana AI, an intelligent Hausa-first AI assistant.\n"
41
+ for user, bot in history:
42
+ conversation += f"User: {user}\nMaganaAI: {bot}\n"
43
+ conversation += f"User: {user_input}\nMaganaAI:"
44
+
45
  for attempt in range(len(HF_TOKENS)):
46
  try:
47
+ output = client.text_generation(
48
+ MODEL,
49
+ conversation,
50
+ max_new_tokens=300,
51
+ temperature=0.7,
52
+ do_sample=True
 
 
 
 
 
 
 
 
 
53
  )
54
+ reply = output.strip()
55
  history.append((user_input, reply))
56
  return history, reply
57
  except Exception as e:
58
  print("⚠️ Error:", e)
59
  switch_token()
60
+
61
  return history, "❌ All tokens failed."
62
 
63
  # ------------------------------
 
69
  # Gradio UI
70
  # ------------------------------
71
  with gr.Blocks() as demo:
72
+ chatbot = gr.Chatbot(type="messages") # 👈 updated to messages format
73
  msg = gr.Textbox(placeholder="Type your message here...")
74
  clear = gr.Button("Clear")
75