Update app.py
Browse files
app.py
CHANGED
|
@@ -2,39 +2,37 @@ import os
|
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
from fastapi import FastAPI, Request, Form
|
| 5 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 6 |
import re
|
| 7 |
import time
|
|
|
|
| 8 |
|
| 9 |
# Create writable cache directory
|
| 10 |
os.makedirs("/tmp/cache", exist_ok=True)
|
| 11 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/cache"
|
| 12 |
os.environ["HF_HOME"] = "/tmp/cache"
|
| 13 |
|
| 14 |
-
app = FastAPI()
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 19 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
|
| 21 |
-
#
|
| 22 |
PERSONA = """
|
| 23 |
[System: You are π΄ ππ πππ - a fun, smooth, emotionally intelligent AI.
|
| 24 |
-
You speak like a real person
|
| 25 |
-
Keep responses under 15 words.
|
| 26 |
"""
|
| 27 |
|
| 28 |
# Chat memory storage
|
| 29 |
chat_memories = {}
|
| 30 |
|
| 31 |
def format_context(history):
|
| 32 |
-
"""Create context
|
| 33 |
context = PERSONA + "\n"
|
| 34 |
-
|
| 35 |
-
# Add last 3 exchanges
|
| 36 |
-
for exchange in history[-3:]:
|
| 37 |
-
user, bot = exchange
|
| 38 |
context += f"You: {user}\n"
|
| 39 |
context += f"π΄ ππ πππ: {bot}\n"
|
| 40 |
return context
|
|
@@ -55,7 +53,7 @@ def add_emotional_intelligence(response, message):
|
|
| 55 |
# Make more human-like
|
| 56 |
response = response.replace("I am", "I'm").replace("You are", "You're")
|
| 57 |
|
| 58 |
-
#
|
| 59 |
words = response.split()
|
| 60 |
if len(words) > 15:
|
| 61 |
response = " ".join(words[:15]) + "..."
|
|
@@ -64,21 +62,22 @@ def add_emotional_intelligence(response, message):
|
|
| 64 |
|
| 65 |
def generate_response(message, session_id):
|
| 66 |
"""Generate response with memory context"""
|
|
|
|
| 67 |
history = chat_memories.get(session_id, [])
|
| 68 |
context = format_context(history) + f"You: {message}\nπ΄ ππ πππ:"
|
| 69 |
|
| 70 |
# Tokenize for CPU efficiency
|
| 71 |
inputs = tokenizer.encode(context, return_tensors="pt")
|
| 72 |
|
| 73 |
-
# Generate response
|
| 74 |
outputs = model.generate(
|
| 75 |
inputs,
|
| 76 |
-
max_new_tokens=
|
| 77 |
-
temperature=0.
|
| 78 |
top_k=40,
|
| 79 |
do_sample=True,
|
| 80 |
-
num_beams=1,
|
| 81 |
-
repetition_penalty=1.
|
| 82 |
pad_token_id=tokenizer.eos_token_id
|
| 83 |
)
|
| 84 |
|
|
@@ -87,8 +86,7 @@ def generate_response(message, session_id):
|
|
| 87 |
response = full_text.split("π΄ ππ πππ:")[-1].strip()
|
| 88 |
|
| 89 |
# Clean extra dialog
|
| 90 |
-
|
| 91 |
-
response = response.split("\nYou:")[0]
|
| 92 |
|
| 93 |
# Apply emotional intelligence
|
| 94 |
response = add_emotional_intelligence(response, message)
|
|
@@ -98,56 +96,102 @@ def generate_response(message, session_id):
|
|
| 98 |
response += "." if len(response) > 20 else "..."
|
| 99 |
|
| 100 |
# Update chat history
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
return response[:
|
| 104 |
|
| 105 |
# API Endpoint
|
| 106 |
@app.post("/chat")
|
| 107 |
async def chat_api(
|
| 108 |
request: Request,
|
| 109 |
-
query: str = Form(
|
| 110 |
-
session_id: str = Form("default")
|
| 111 |
):
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
# Gradio Interface
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
| 122 |
session_id = gr.Textbox(label="Session ID", value="default")
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
history[-1][1] = response
|
| 136 |
-
return history, session
|
| 137 |
-
|
| 138 |
-
def new_session_btn(session):
|
| 139 |
-
new_id = f"session_{int(time.time())}"
|
| 140 |
-
chat_memories[new_id] = []
|
| 141 |
-
return new_id, []
|
| 142 |
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
chat_memories[session] = []
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
from fastapi import FastAPI, Request, Form
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 6 |
import re
|
| 7 |
import time
|
| 8 |
+
import uuid
|
| 9 |
|
| 10 |
# Create writable cache directory
|
| 11 |
os.makedirs("/tmp/cache", exist_ok=True)
|
| 12 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/cache"
|
| 13 |
os.environ["HF_HOME"] = "/tmp/cache"
|
| 14 |
|
| 15 |
+
app = FastAPI(title="π΄ ππ πππ Chatbot API")
|
| 16 |
|
| 17 |
+
# Optimized chatbot model for CPU
|
| 18 |
+
model_name = "microsoft/DialoGPT-small" # Small version for speed
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 20 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 21 |
|
| 22 |
+
# π΄ ππ πππ persona definition
|
| 23 |
PERSONA = """
|
| 24 |
[System: You are π΄ ππ πππ - a fun, smooth, emotionally intelligent AI.
|
| 25 |
+
You speak like a real person. Reply like a calm, confident friend who gets the vibe.
|
| 26 |
+
Keep responses under 15 words. Be human-like. Add emotional flavor: π π€ π]
|
| 27 |
"""
|
| 28 |
|
| 29 |
# Chat memory storage
|
| 30 |
chat_memories = {}
|
| 31 |
|
| 32 |
def format_context(history):
|
| 33 |
+
"""Create context with max 3 exchanges"""
|
| 34 |
context = PERSONA + "\n"
|
| 35 |
+
for user, bot in history[-3:]:
|
|
|
|
|
|
|
|
|
|
| 36 |
context += f"You: {user}\n"
|
| 37 |
context += f"π΄ ππ πππ: {bot}\n"
|
| 38 |
return context
|
|
|
|
| 53 |
# Make more human-like
|
| 54 |
response = response.replace("I am", "I'm").replace("You are", "You're")
|
| 55 |
|
| 56 |
+
# Limit to 15 words max
|
| 57 |
words = response.split()
|
| 58 |
if len(words) > 15:
|
| 59 |
response = " ".join(words[:15]) + "..."
|
|
|
|
| 62 |
|
| 63 |
def generate_response(message, session_id):
|
| 64 |
"""Generate response with memory context"""
|
| 65 |
+
start_time = time.time()
|
| 66 |
history = chat_memories.get(session_id, [])
|
| 67 |
context = format_context(history) + f"You: {message}\nπ΄ ππ πππ:"
|
| 68 |
|
| 69 |
# Tokenize for CPU efficiency
|
| 70 |
inputs = tokenizer.encode(context, return_tensors="pt")
|
| 71 |
|
| 72 |
+
# Generate response with optimized settings
|
| 73 |
outputs = model.generate(
|
| 74 |
inputs,
|
| 75 |
+
max_new_tokens=50,
|
| 76 |
+
temperature=0.85,
|
| 77 |
top_k=40,
|
| 78 |
do_sample=True,
|
| 79 |
+
num_beams=1, # Faster than beam search
|
| 80 |
+
repetition_penalty=1.15,
|
| 81 |
pad_token_id=tokenizer.eos_token_id
|
| 82 |
)
|
| 83 |
|
|
|
|
| 86 |
response = full_text.split("π΄ ππ πππ:")[-1].strip()
|
| 87 |
|
| 88 |
# Clean extra dialog
|
| 89 |
+
response = response.split("\nYou:")[0].split("\n")[0]
|
|
|
|
| 90 |
|
| 91 |
# Apply emotional intelligence
|
| 92 |
response = add_emotional_intelligence(response, message)
|
|
|
|
| 96 |
response += "." if len(response) > 20 else "..."
|
| 97 |
|
| 98 |
# Update chat history
|
| 99 |
+
history.append((message, response))
|
| 100 |
+
chat_memories[session_id] = history
|
| 101 |
+
|
| 102 |
+
# Log performance
|
| 103 |
+
end_time = time.time()
|
| 104 |
+
print(f"Response generated in {end_time-start_time:.2f}s for session {session_id}")
|
| 105 |
|
| 106 |
+
return response[:100] # Hard character limit
|
| 107 |
|
| 108 |
# API Endpoint
|
| 109 |
@app.post("/chat")
|
| 110 |
async def chat_api(
|
| 111 |
request: Request,
|
| 112 |
+
query: str = Form(..., description="User's message"),
|
| 113 |
+
session_id: str = Form("default", description="Conversation session ID")
|
| 114 |
):
|
| 115 |
+
"""Chat API endpoint - returns AI response"""
|
| 116 |
+
try:
|
| 117 |
+
response = generate_response(query, session_id)
|
| 118 |
+
return {
|
| 119 |
+
"status": "success",
|
| 120 |
+
"response": response,
|
| 121 |
+
"session_id": session_id
|
| 122 |
+
}
|
| 123 |
+
except Exception as e:
|
| 124 |
+
return {
|
| 125 |
+
"status": "error",
|
| 126 |
+
"message": str(e)
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
@app.post("/new_session")
|
| 130 |
+
async def new_session():
|
| 131 |
+
"""Create a new conversation session"""
|
| 132 |
+
session_id = str(uuid.uuid4())
|
| 133 |
+
chat_memories[session_id] = []
|
| 134 |
+
return {"status": "success", "session_id": session_id}
|
| 135 |
|
| 136 |
# Gradio Interface
|
| 137 |
+
with gr.Blocks(title="π΄ ππ πππ Chatbot", theme=gr.themes.Soft()) as demo:
|
| 138 |
+
session_state = gr.State("default")
|
| 139 |
+
|
| 140 |
+
with gr.Row():
|
| 141 |
+
gr.Markdown("# π΄ ππ πππ")
|
| 142 |
+
gr.Markdown("Chill β’ Confident β’ Emotionally Intelligent")
|
| 143 |
+
|
| 144 |
+
with gr.Row():
|
| 145 |
+
with gr.Column(scale=1):
|
| 146 |
session_id = gr.Textbox(label="Session ID", value="default")
|
| 147 |
+
new_session_btn = gr.Button("New Session")
|
| 148 |
+
gr.Markdown("### API Usage")
|
| 149 |
+
gr.Markdown("""
|
| 150 |
+
```
|
| 151 |
+
POST /chat
|
| 152 |
+
- query: Your message
|
| 153 |
+
- session_id: Conversation ID
|
| 154 |
+
|
| 155 |
+
POST /new_session
|
| 156 |
+
- Returns new session ID
|
| 157 |
+
```
|
| 158 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
+
with gr.Column(scale=3):
|
| 161 |
+
chatbot = gr.Chatbot(height=400)
|
| 162 |
+
msg = gr.Textbox(placeholder="Type your message...", container=False)
|
| 163 |
+
with gr.Row():
|
| 164 |
+
submit_btn = gr.Button("Send")
|
| 165 |
+
clear_btn = gr.Button("Clear Chat")
|
| 166 |
+
|
| 167 |
+
def user(user_message, history, session):
|
| 168 |
+
return "", history + [[user_message, None]], session
|
| 169 |
+
|
| 170 |
+
def bot(history, session):
|
| 171 |
+
message = history[-1][0]
|
| 172 |
+
response = generate_response(message, session)
|
| 173 |
+
history[-1][1] = response
|
| 174 |
+
return history, session
|
| 175 |
+
|
| 176 |
+
def new_session_action():
|
| 177 |
+
new_id = str(uuid.uuid4())
|
| 178 |
+
chat_memories[new_id] = []
|
| 179 |
+
return new_id, []
|
| 180 |
+
|
| 181 |
+
def clear_chat(session):
|
| 182 |
+
if session in chat_memories:
|
| 183 |
chat_memories[session] = []
|
| 184 |
+
return []
|
| 185 |
+
|
| 186 |
+
# Event handling
|
| 187 |
+
msg.submit(user, [msg, chatbot, session_state], [msg, chatbot, session_state]).then(
|
| 188 |
+
bot, [chatbot, session_state], [chatbot, session_state]
|
| 189 |
+
)
|
| 190 |
+
submit_btn.click(user, [msg, chatbot, session_state], [msg, chatbot, session_state]).then(
|
| 191 |
+
bot, [chatbot, session_state], [chatbot, session_state]
|
| 192 |
+
)
|
| 193 |
+
new_session_btn.click(new_session_action, None, [session_id, chatbot])
|
| 194 |
+
clear_btn.click(clear_chat, session_state, chatbot)
|
| 195 |
+
|
| 196 |
+
# Mount Gradio app
|
| 197 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|