camawowo commited on
Commit
d69a167
·
1 Parent(s): 410e4ab

fix gradio history format bug

Browse files
Files changed (1) hide show
  1. app.py +30 -12
app.py CHANGED
@@ -1,32 +1,52 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Load Phi-3 Mini model
5
  model = pipeline(
6
  "text-generation",
7
  model="microsoft/Phi-3-mini-4k-instruct",
8
  device_map="auto"
9
  )
10
 
11
- # Build proper conversation prompt
12
  def build_prompt(history, user_input):
13
  prompt = ""
14
 
15
- for user_msg, bot_msg in history:
16
- prompt += f"<|user|>\n{user_msg}\n<|assistant|>\n{bot_msg}\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  prompt += f"<|user|>\n{user_input}\n<|assistant|>\n"
19
 
20
  return prompt
21
 
22
 
23
- # Chat function
24
  def chat(message, history):
 
25
  try:
26
- # Create formatted prompt
27
  prompt = build_prompt(history, message)
28
 
29
- # Generate response
30
  response = model(
31
  prompt,
32
  max_new_tokens=150,
@@ -37,10 +57,9 @@ def chat(message, history):
37
  return_full_text=False
38
  )
39
 
40
- # Extract text safely
41
  answer = response[0]["generated_text"].strip()
42
 
43
- # Remove unwanted continuation text
44
  stop_tokens = [
45
  "<|user|>",
46
  "<|assistant|>",
@@ -52,7 +71,6 @@ def chat(message, history):
52
  if token in answer:
53
  answer = answer.split(token)[0].strip()
54
 
55
- # Safety fallback
56
  if not answer:
57
  answer = "I could not generate a response."
58
 
@@ -62,11 +80,11 @@ def chat(message, history):
62
  return f"Error: {str(e)}"
63
 
64
 
65
- # Create chat interface
66
  demo = gr.ChatInterface(
67
  fn=chat,
68
  title="Symbolic Alpha AI Agent",
69
- description="Phi-3 Mini AI chatbot running on Hugging Face Spaces"
70
  )
71
 
72
  # Launch app
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # Load model
5
  model = pipeline(
6
  "text-generation",
7
  model="microsoft/Phi-3-mini-4k-instruct",
8
  device_map="auto"
9
  )
10
 
11
+ # Build conversation prompt safely
12
  def build_prompt(history, user_input):
13
  prompt = ""
14
 
15
+ try:
16
+ for item in history:
17
+
18
+ # Handle new Gradio message format
19
+ if isinstance(item, dict):
20
+ role = item.get("role", "")
21
+ content = item.get("content", "")
22
+
23
+ if role == "user":
24
+ prompt += f"<|user|>\n{content}\n"
25
+
26
+ elif role == "assistant":
27
+ prompt += f"<|assistant|>\n{content}\n"
28
+
29
+ # Handle old tuple format
30
+ elif isinstance(item, (list, tuple)) and len(item) == 2:
31
+ user_msg, bot_msg = item
32
+
33
+ prompt += f"<|user|>\n{user_msg}\n"
34
+ prompt += f"<|assistant|>\n{bot_msg}\n"
35
+
36
+ except Exception:
37
+ pass
38
 
39
  prompt += f"<|user|>\n{user_input}\n<|assistant|>\n"
40
 
41
  return prompt
42
 
43
 
44
+ # Main chatbot function
45
  def chat(message, history):
46
+
47
  try:
 
48
  prompt = build_prompt(history, message)
49
 
 
50
  response = model(
51
  prompt,
52
  max_new_tokens=150,
 
57
  return_full_text=False
58
  )
59
 
 
60
  answer = response[0]["generated_text"].strip()
61
 
62
+ # Remove unwanted continuation
63
  stop_tokens = [
64
  "<|user|>",
65
  "<|assistant|>",
 
71
  if token in answer:
72
  answer = answer.split(token)[0].strip()
73
 
 
74
  if not answer:
75
  answer = "I could not generate a response."
76
 
 
80
  return f"Error: {str(e)}"
81
 
82
 
83
+ # Create UI
84
  demo = gr.ChatInterface(
85
  fn=chat,
86
  title="Symbolic Alpha AI Agent",
87
+ description="Stable Phi-3 Mini chatbot running on Hugging Face Spaces"
88
  )
89
 
90
  # Launch app