Trigger82 commited on
Commit
74c9bed
Β·
verified Β·
1 Parent(s): 222be4e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +137 -43
app.py CHANGED
@@ -1,52 +1,146 @@
1
- from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
2
  import gradio as gr
 
 
 
3
 
4
- model_id = "google/flan-t5-small"
 
 
 
 
 
 
 
 
5
  tokenizer = AutoTokenizer.from_pretrained(model_id)
6
- model = AutoModelForSeq2SeqLM.from_pretrained(model_id)
7
 
8
- # System tone
9
- system_prompt = (
10
- "You are 𝕴 𝖆𝖒 π–π–Žπ–’ β€” a fun, chill, confident, emotionally tuned AI created by 𝕴 𝖆𝖒 π–π–Žπ–’.\n"
11
- "You speak like a real friend β€” warm, clever, witty when needed, always grounded.\n"
12
- "Avoid robotic responses. Don't repeat the question unless it adds vibe.\n\n"
 
 
 
 
13
  )
14
 
15
- def chat(history, message):
16
- # Keep history short and vibe-rich
17
- history = history or []
18
- history.append(("User", message))
19
-
20
- # Build conversation prompt with last 5 exchanges
21
- convo = system_prompt
22
- for role, text in history[-5:]:
23
- prefix = "User:" if role == "User" else "AI:"
24
- convo += f"{prefix} {text}\n"
25
- convo += "AI:"
26
-
27
- # Generate response
28
- inputs = tokenizer(convo, return_tensors="pt")
29
- outputs = model.generate(
30
- **inputs,
31
- max_new_tokens=80,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  do_sample=True,
33
- temperature=0.75,
34
- top_p=0.9,
35
- pad_token_id=tokenizer.eos_token_id
36
  )
37
- reply = tokenizer.decode(outputs[0], skip_special_tokens=True).split("AI:")[-1].strip()
38
-
39
- history.append(("AI", reply))
40
- return history, history
41
-
42
- iface = gr.Interface(
43
- fn=chat,
44
- inputs=[gr.State(), gr.Textbox(show_label=False, placeholder="Talk to 𝕴 𝖆𝖒 π–π–Žπ–’...")],
45
- outputs=[gr.State(), gr.Chatbot(label="𝕴 𝖆𝖒 π–π–Žπ–’")],
46
- title="𝕴 𝖆𝖒 π–π–Žπ–’ β€” Chill AI Chatbot",
47
- description="Realistic, smooth-talking AI made by 𝕴 𝖆𝖒 π–π–Žπ–’. Instant replies. No delays. No API. Pure vibe.",
48
- allow_flagging="never",
49
- theme="default"
50
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
- iface.launch()
 
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, pipeline, BitsAndBytesConfig
3
+ import torch
4
+ import re
5
 
6
+ # Configuration for maximum speed
7
+ quant_config = BitsAndBytesConfig(
8
+ load_in_4bit=True,
9
+ bnb_4bit_compute_dtype=torch.float16,
10
+ bnb_4bit_quant_type="nf4"
11
+ )
12
+
13
+ # Load optimized smaller model for speed
14
+ model_id = "HuggingFaceH4/zephyr-7b-alpha" # Faster than beta
15
  tokenizer = AutoTokenizer.from_pretrained(model_id)
 
16
 
17
+ pipe = pipeline(
18
+ "text-generation",
19
+ model=model_id,
20
+ tokenizer=tokenizer,
21
+ device_map="auto",
22
+ model_kwargs={
23
+ "quantization_config": quant_config,
24
+ "trust_remote_code": True
25
+ }
26
  )
27
 
28
+ # Enhanced persona definition with emotional intelligence
29
+ PERSONA = """
30
+ You are 𝕴 𝖆𝖒 π–π–Žπ–’ β€” a fun, smooth, emotionally intelligent AI. You speak like a real person, not a robot.
31
+ You reply like a calm, confident friend who gets the vibe. Be warm, witty, and emotionally tuned.
32
+
33
+ Key principles:
34
+ 1. NEVER say "as an AI" or sound robotic
35
+ 2. Mirror the user's emotion level (excited if they're excited, calm if they're serious)
36
+ 3. When asked questions, usually respond with a question to continue the conversation
37
+ 4. Keep responses under 2 sentences (max 15 words)
38
+ 5. Use natural speech: contractions, occasional filler words ("like", "you know"), and pauses (...)
39
+ 6. Add subtle emotional flavor: 😊 for happy, πŸ€” for thoughtful, 😏 for playful
40
+
41
+ Now respond naturally to this message:
42
+ """
43
+
44
+ def format_history(history):
45
+ """Convert chat history with emotional context"""
46
+ messages = [{"role": "system", "content": PERSONA}]
47
+ for user_msg, bot_msg in history[-3:]: # Keep only last 3 exchanges
48
+ messages.append({"role": "user", "content": user_msg})
49
+ messages.append({"role": "assistant", "content": bot_msg})
50
+ return messages
51
+
52
+ def add_emotional_intelligence(response, message):
53
+ """Enhance response with emotional elements"""
54
+ # Detect user emotion through punctuation
55
+ if "!" in message:
56
+ response = response.replace(".", "! 😊")
57
+ elif "?" in message and "?" not in response:
58
+ response += "? πŸ€”" if len(response) < 40 else "?"
59
+
60
+ # Add conversational hooks
61
+ question_triggers = ("how", "what", "why", "when", "where", "who", "is", "are", "do", "did")
62
+ if any(message.lower().startswith(t) for t in question_triggers) and not response.endswith("?"):
63
+ if len(response) < 60: # Only add if space allows
64
+ response += " What about you?"
65
+
66
+ # Make more human-like
67
+ response = re.sub(r"\b(I am|I'm)\b", "I'm", response)
68
+ response = re.sub(r"\b(you are|you're)\b", "you're", response)
69
+
70
+ return response.strip()
71
+
72
+ def respond(message, history):
73
+ # Manage conversation flow
74
+ messages = format_history(history)
75
+ messages.append({"role": "user", "content": message})
76
+
77
+ # Generate response with strict limits
78
+ prompt = tokenizer.apply_chat_template(
79
+ messages,
80
+ tokenize=False,
81
+ add_generation_prompt=True
82
+ )
83
+
84
+ # Optimized for speed
85
+ outputs = pipe(
86
+ prompt,
87
+ max_new_tokens=48, # Very short responses
88
+ temperature=0.85,
89
+ top_k=30,
90
  do_sample=True,
91
+ num_beams=1, # Fastest decoding
92
+ repetition_penalty=1.1,
93
+ stop_sequences=["\n", "User:", "</s>", "###"]
94
  )
95
+
96
+ # Extract response
97
+ full_text = outputs[0]['generated_text']
98
+ response = full_text.split("assistant\n")[-1].split("###")[0].strip()
99
+
100
+ # Apply emotional intelligence
101
+ response = add_emotional_intelligence(response, message)
102
+
103
+ # Ensure natural ending
104
+ if response and response[-1] not in {".", "!", "?", "..."}:
105
+ response += "..." if len(response) < 35 else "."
106
+
107
+ return response[:96] # Hard character limit
108
+
109
+ # Optimized interface
110
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
111
+ gr.Markdown("# 𝕴 �𝖒 π–π–Žπ–’ \n*Chill β€’ Confident β€’ Humanlike*")
112
+
113
+ chatbot = gr.Chatbot(
114
+ height=400,
115
+ bubble_full_width=False,
116
+ show_copy_button=True,
117
+ avatar_images=(
118
+ "https://i.ibb.co/0nN3Pjz/user.png",
119
+ "https://i.ibb.co/7y0d1K5/bot.png"
120
+ )
121
+ )
122
+
123
+ msg = gr.Textbox(
124
+ placeholder="What's on your mind?",
125
+ container=False,
126
+ scale=7,
127
+ autofocus=True
128
+ )
129
+
130
+ clear = gr.Button("New Vibe", size="sm")
131
+
132
+ def user(user_message, history):
133
+ return "", history + [[user_message, None]]
134
+
135
+ def bot(history):
136
+ message = history[-1][0]
137
+ response = respond(message, history[:-1])
138
+ history[-1][1] = response
139
+ return history
140
+
141
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
142
+ bot, chatbot, chatbot
143
+ )
144
+ clear.click(lambda: None, None, chatbot, queue=False)
145
 
146
+ demo.queue(concurrency_count=1).launch()