Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, Form
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
import re
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
# Load model
|
| 9 |
+
model_id = "microsoft/DialoGPT-medium"
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 12 |
+
|
| 13 |
+
# Chat memory storage
|
| 14 |
+
chat_memories = {}
|
| 15 |
+
|
| 16 |
+
# Persona definition
|
| 17 |
+
PERSONA = """
|
| 18 |
+
[System: You are π΄ ππ πππ - a fun, smooth, emotionally intelligent AI.
|
| 19 |
+
You speak like a real person, not a robot. Reply like a calm, confident friend who gets the vibe.
|
| 20 |
+
Keep responses under 15 words. Use natural speech. Add emotional flavor: π π€ π]
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
def format_context(history):
|
| 24 |
+
context = PERSONA + "\n"
|
| 25 |
+
for user, bot in history[-3:]:
|
| 26 |
+
context += f"You: {user}\n"
|
| 27 |
+
context += f"π΄ ππ πππ: {bot}\n"
|
| 28 |
+
return context
|
| 29 |
+
|
| 30 |
+
def add_emotional_intelligence(response, message):
|
| 31 |
+
if "!" in message or any(w in response.lower() for w in ["cool", "great", "love", "awesome"]):
|
| 32 |
+
response += " π"
|
| 33 |
+
elif "?" in message or any(w in response.lower() for w in ["think", "why", "how", "consider"]):
|
| 34 |
+
response += " π€"
|
| 35 |
+
|
| 36 |
+
if "?" in message and not response.endswith("?"):
|
| 37 |
+
if len(response.split()) < 10:
|
| 38 |
+
response += " What do you think?"
|
| 39 |
+
|
| 40 |
+
response = response.replace("I am", "I'm").replace("You are", "You're")
|
| 41 |
+
words = response.split()
|
| 42 |
+
return " ".join(words[:15]) + "..." if len(words) > 15 else response
|
| 43 |
+
|
| 44 |
+
def generate_response(message, session_id):
|
| 45 |
+
history = chat_memories.get(session_id, [])
|
| 46 |
+
context = format_context(history) + f"You: {message}\nπ΄ ππ πππ:"
|
| 47 |
+
|
| 48 |
+
inputs = tokenizer.encode(context, return_tensors="pt")
|
| 49 |
+
outputs = model.generate(
|
| 50 |
+
inputs,
|
| 51 |
+
max_new_tokens=48,
|
| 52 |
+
temperature=0.9,
|
| 53 |
+
top_k=40,
|
| 54 |
+
do_sample=True,
|
| 55 |
+
num_beams=1,
|
| 56 |
+
repetition_penalty=1.1,
|
| 57 |
+
pad_token_id=tokenizer.eos_token_id
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
full_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 61 |
+
response = full_text.split("π΄ ππ πππ:")[-1].strip()
|
| 62 |
+
|
| 63 |
+
if "\nYou:" in response:
|
| 64 |
+
response = response.split("\nYou:")[0]
|
| 65 |
+
|
| 66 |
+
response = add_emotional_intelligence(response, message)
|
| 67 |
+
|
| 68 |
+
if response and response[-1] not in {".", "!", "?", "..."}:
|
| 69 |
+
response += "." if len(response) > 20 else "..."
|
| 70 |
+
|
| 71 |
+
# Update chat history
|
| 72 |
+
chat_memories[session_id] = history + [[message, response]]
|
| 73 |
+
|
| 74 |
+
return response[:80]
|
| 75 |
+
|
| 76 |
+
# API Endpoint
|
| 77 |
+
@app.post("/chat")
|
| 78 |
+
async def chat_api(
|
| 79 |
+
request: Request,
|
| 80 |
+
query: str = Form(...),
|
| 81 |
+
session_id: str = Form("default")
|
| 82 |
+
):
|
| 83 |
+
response = generate_response(query, session_id)
|
| 84 |
+
return {"response": response, "session_id": session_id}
|
| 85 |
+
|
| 86 |
+
# Gradio Interface
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
import gradio as gr
|
| 89 |
+
|
| 90 |
+
with gr.Blocks(title="π΄ ππ πππ", theme=gr.themes.Soft()) as demo:
|
| 91 |
+
session_state = gr.State("default")
|
| 92 |
+
gr.Markdown("# π΄ ππ πππ Chat API")
|
| 93 |
+
|
| 94 |
+
with gr.Row():
|
| 95 |
+
session_id = gr.Textbox(label="Session ID", value="default")
|
| 96 |
+
new_session = gr.Button("New Session")
|
| 97 |
+
|
| 98 |
+
chatbot = gr.Chatbot(height=300)
|
| 99 |
+
msg = gr.Textbox(label="Your Message")
|
| 100 |
+
clear = gr.Button("Clear Chat")
|
| 101 |
+
|
| 102 |
+
def user(user_message, history, session):
|
| 103 |
+
return "", history + [[user_message, None]], session
|
| 104 |
+
|
| 105 |
+
def bot(history, session):
|
| 106 |
+
message = history[-1][0]
|
| 107 |
+
response = generate_response(message, session)
|
| 108 |
+
history[-1][1] = response
|
| 109 |
+
return history, session
|
| 110 |
+
|
| 111 |
+
def new_session_btn(session):
|
| 112 |
+
new_id = f"session_{int(time.time())}"
|
| 113 |
+
chat_memories[new_id] = []
|
| 114 |
+
return new_id, []
|
| 115 |
+
|
| 116 |
+
def clear_chat(session):
|
| 117 |
+
chat_memories[session] = []
|
| 118 |
+
return []
|
| 119 |
+
|
| 120 |
+
msg.submit(user, [msg, chatbot, session_state], [msg, chatbot, session_state]).then(
|
| 121 |
+
bot, [chatbot, session_state], [chatbot, session_state]
|
| 122 |
+
)
|
| 123 |
+
new_session.click(new_session_btn, session_state, [session_id, chatbot])
|
| 124 |
+
clear.click(clear_chat, session_state, chatbot)
|
| 125 |
+
|
| 126 |
+
demo.launch(server_port=7860, server_name="0.0.0.0")
|