itsMaaz commited on
Commit
8b6b42e
·
verified ·
1 Parent(s): 7bb2580

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -33
app.py CHANGED
@@ -1,59 +1,51 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Better chat model (trained for dialogue)
 
 
 
 
 
 
 
 
5
  MODEL = "facebook/blenderbot-1B-distill"
6
  chatbot = pipeline("text2text-generation", model=MODEL)
7
 
8
  def chat_function(message, history):
9
  """Natural conversation that works for any input"""
10
  try:
11
- # Build conversation with proper format
12
- # Keep last 2 exchanges for context
13
- conv_lines = []
14
  for human, bot in history[-2:]:
15
- conv_lines.append(f"Human: {human}")
16
- conv_lines.append(f"Bot: {bot}")
17
- conv_lines.append(f"Human: {message}")
18
- conv_lines.append("Bot:")
19
-
20
- prompt = "\n".join(conv_lines)
21
-
22
- # Generate response
23
  result = chatbot(
24
  prompt,
25
- max_length=120,
26
- temperature=0.9,
27
  do_sample=True,
28
  top_p=0.9
29
  )
30
-
31
- # Extract bot's response
32
- response = result[0]["generated_text"]
33
-
34
- # Get only the part after "Bot:"
35
- if "Bot:" in response:
36
- bot_response = response.split("Bot:")[-1].strip()
37
- # Remove any trailing conversation markers
38
- bot_response = bot_response.split("Human:")[0].strip()
39
- return bot_response if bot_response else "Tell me more!"
40
-
41
- # If format is unexpected, return the response directly
42
- return response.strip()
43
-
44
  except Exception as e:
45
  print(f"Error: {e}")
46
- # Generate without history on error
47
- fallback = chatbot(message, max_length=60)[0]["generated_text"]
48
- return fallback.strip()
49
 
50
  demo = gr.ChatInterface(
51
  fn=chat_function,
52
  title="-Airi-",
53
- description="Your friendly AI assistant - ask me anything!",
54
  examples=["Hello!", "What's up?", "Tell me a joke"],
55
  cache_examples=False
56
  )
57
 
58
  if __name__ == "__main__":
59
- demo.launch()
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # --------------------------------------------------
5
+ # PERSONALITY – edit this string to change Airi’s vibe
6
+ # --------------------------------------------------
7
+ AI_PERSONA = (
8
+ "You are Airi, a cheerful, curious, slightly sarcastic AI. "
9
+ "Keep answers short, use emoji now and then, and act like a friend in a chat app. "
10
+ "If you don’t know something, joke about it. "
11
+ )
12
+
13
  MODEL = "facebook/blenderbot-1B-distill"
14
  chatbot = pipeline("text2text-generation", model=MODEL)
15
 
16
  def chat_function(message, history):
17
  """Natural conversation that works for any input"""
18
  try:
19
+ # Build prompt with persona injected
20
+ prompt = AI_PERSONA + "\n"
 
21
  for human, bot in history[-2:]:
22
+ prompt += f"Human: {human}\nBot: {bot}\n"
23
+ prompt += f"Human: {message}\nBot:"
24
+
 
 
 
 
 
25
  result = chatbot(
26
  prompt,
27
+ max_length=140,
28
+ temperature=0.8,
29
  do_sample=True,
30
  top_p=0.9
31
  )
32
+
33
+ response = result[0]["generated_text"].split("Bot:")[-1].split("Human:")[0].strip()
34
+ return response if response else "😅 idk, tell me more!"
35
+
 
 
 
 
 
 
 
 
 
 
36
  except Exception as e:
37
  print(f"Error: {e}")
38
+ fallback = chatbot(message, max_length=60)[0]["generated_text"].strip()
39
+ return fallback
 
40
 
41
  demo = gr.ChatInterface(
42
  fn=chat_function,
43
  title="-Airi-",
44
+ description="Your friendly AI assistant ask me anything!",
45
  examples=["Hello!", "What's up?", "Tell me a joke"],
46
  cache_examples=False
47
  )
48
 
49
  if __name__ == "__main__":
50
+ demo.launch()
51
+