Update app.py
Browse files
app.py
CHANGED
|
@@ -1,79 +1,51 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from fastapi import FastAPI, Query
|
| 3 |
-
from fastapi.middleware.wsgi import WSGIMiddleware
|
| 4 |
-
import uvicorn
|
| 5 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 6 |
import torch
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
# Load model and tokenizer
|
| 9 |
-
model_id = "microsoft/
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
[System: You are π΄ ππ πππ - a fun, smooth, emotionally intelligent AI.
|
| 16 |
-
You speak like a real person, not a robot. Keep it under 15 words. ππ]
|
| 17 |
-
"""
|
| 18 |
|
|
|
|
| 19 |
def format_context(history):
|
| 20 |
-
context =
|
| 21 |
-
|
| 22 |
-
return context
|
| 23 |
-
# Use only last 3 exchanges to keep it short
|
| 24 |
-
for user, bot in history[-3:]:
|
| 25 |
context += f"You: {user}\nπ΄ ππ πππ: {bot}\n"
|
| 26 |
return context
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
return " ".join(resp.split()[:15])
|
| 34 |
-
|
| 35 |
-
def generate_ai_reply(user_input, history):
|
| 36 |
-
context = format_context(history) + f"You: {user_input}\nπ΄ ππ πππ:"
|
| 37 |
-
inputs = tokenizer.encode(context, return_tensors="pt", truncation=True, max_length=1024)
|
| 38 |
-
|
| 39 |
-
outputs = model.generate(
|
| 40 |
-
inputs,
|
| 41 |
-
max_new_tokens=50,
|
| 42 |
-
temperature=0.9,
|
| 43 |
-
top_k=40,
|
| 44 |
-
do_sample=True,
|
| 45 |
-
pad_token_id=tokenizer.eos_token_id
|
| 46 |
-
)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
response = enhance_response(response, user_input)
|
| 51 |
-
return response
|
| 52 |
|
| 53 |
-
|
|
|
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
reply = generate_ai_reply(query, history=[])
|
| 60 |
-
return {"reply": reply}
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
def chat(user_input, history):
|
| 64 |
-
history = history or []
|
| 65 |
-
reply = generate_ai_reply(user_input, history)
|
| 66 |
history.append((user_input, reply))
|
| 67 |
-
|
| 68 |
|
| 69 |
-
|
| 70 |
-
chatbot = gr.Chatbot()
|
| 71 |
-
msg = gr.Textbox(placeholder="Say something...")
|
| 72 |
-
state = gr.State()
|
| 73 |
-
msg.submit(chat, inputs=[msg, state], outputs=[chatbot, state])
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
|
| 79 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
import urllib.parse
|
| 5 |
|
| 6 |
+
# Load model and tokenizer
|
| 7 |
+
model_id = "microsoft/phi-2"
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 9 |
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 10 |
|
| 11 |
+
# Global memory for all users
|
| 12 |
+
chat_history = {}
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Format past messages
|
| 15 |
def format_context(history):
|
| 16 |
+
context = ""
|
| 17 |
+
for user, bot in history[-3:]: # Last 3 exchanges
|
|
|
|
|
|
|
|
|
|
| 18 |
context += f"You: {user}\nπ΄ ππ πππ: {bot}\n"
|
| 19 |
return context
|
| 20 |
|
| 21 |
+
# Main chat function with memory per user
|
| 22 |
+
def chat_with_memory(query_string):
|
| 23 |
+
parsed = urllib.parse.parse_qs(query_string)
|
| 24 |
+
user_input = parsed.get("query", [""])[0]
|
| 25 |
+
user_id = parsed.get("user_id", ["default"])[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# Get or init user history
|
| 28 |
+
history = chat_history.get(user_id, [])
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# Format prompt
|
| 31 |
+
context = format_context(history) + f"You: {user_input}\nπ΄ ππ πππ:"
|
| 32 |
|
| 33 |
+
# Tokenize & generate
|
| 34 |
+
inputs = tokenizer(context, return_tensors="pt", return_attention_mask=True)
|
| 35 |
+
outputs = model.generate(**inputs, max_new_tokens=100, pad_token_id=tokenizer.eos_token_id)
|
| 36 |
+
reply = tokenizer.decode(outputs[0], skip_special_tokens=True).split("π΄ ππ πππ:")[-1].strip()
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# Save memory
|
|
|
|
|
|
|
|
|
|
| 39 |
history.append((user_input, reply))
|
| 40 |
+
chat_history[user_id] = history[-10:]
|
| 41 |
|
| 42 |
+
return {"reply": reply}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
# Create public /ai?query=&user_id=
|
| 45 |
+
iface = gr.Interface(
|
| 46 |
+
fn=chat_with_memory,
|
| 47 |
+
inputs="text", # URL query string
|
| 48 |
+
outputs="json"
|
| 49 |
+
)
|
| 50 |
|
| 51 |
+
iface.launch()
|
|
|