Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,21 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
from fastapi import FastAPI, Request
|
| 5 |
from fastapi.responses import JSONResponse
|
| 6 |
-
import
|
| 7 |
|
| 8 |
# Load model
|
| 9 |
model_id = "microsoft/phi-2"
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 12 |
|
| 13 |
-
# Memory
|
| 14 |
chat_history = {}
|
| 15 |
|
| 16 |
-
#
|
| 17 |
def format_context(history):
|
| 18 |
return "".join([f"You: {u}\nπ΄ ππ πππ: {b}\n" for u, b in history[-3:]])
|
| 19 |
|
| 20 |
-
# FastAPI app
|
| 21 |
app = FastAPI()
|
| 22 |
|
| 23 |
@app.get("/ai")
|
|
@@ -26,23 +24,17 @@ async def ai_chat(request: Request):
|
|
| 26 |
user_input = query_params.get("query", "")
|
| 27 |
user_id = query_params.get("user_id", "default")
|
| 28 |
|
| 29 |
-
#
|
| 30 |
history = chat_history.get(user_id, [])
|
| 31 |
prompt = format_context(history) + f"You: {user_input}\nπ΄ ππ πππ:"
|
| 32 |
|
| 33 |
-
#
|
| 34 |
inputs = tokenizer(prompt, 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 |
-
#
|
| 39 |
history.append((user_input, reply))
|
| 40 |
chat_history[user_id] = history[-10:]
|
| 41 |
|
| 42 |
-
return JSONResponse({"reply": reply})
|
| 43 |
-
|
| 44 |
-
# Wrap with Gradio to serve
|
| 45 |
-
app = gr.mount_gradio_app(app, gr.Interface(lambda x: x, "textbox", "textbox"))
|
| 46 |
-
|
| 47 |
-
# Launch it
|
| 48 |
-
gradio_app = gr.FastAPI(app)
|
|
|
|
|
|
|
| 1 |
import torch
|
|
|
|
| 2 |
from fastapi import FastAPI, Request
|
| 3 |
from fastapi.responses import JSONResponse
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
|
| 6 |
# Load model
|
| 7 |
model_id = "microsoft/phi-2"
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 9 |
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 10 |
|
| 11 |
+
# Memory dict
|
| 12 |
chat_history = {}
|
| 13 |
|
| 14 |
+
# History formatter
|
| 15 |
def format_context(history):
|
| 16 |
return "".join([f"You: {u}\nπ΄ ππ πππ: {b}\n" for u, b in history[-3:]])
|
| 17 |
|
| 18 |
+
# Create FastAPI app
|
| 19 |
app = FastAPI()
|
| 20 |
|
| 21 |
@app.get("/ai")
|
|
|
|
| 24 |
user_input = query_params.get("query", "")
|
| 25 |
user_id = query_params.get("user_id", "default")
|
| 26 |
|
| 27 |
+
# Pull history
|
| 28 |
history = chat_history.get(user_id, [])
|
| 29 |
prompt = format_context(history) + f"You: {user_input}\nπ΄ ππ πππ:"
|
| 30 |
|
| 31 |
+
# Run model
|
| 32 |
inputs = tokenizer(prompt, return_tensors="pt", return_attention_mask=True)
|
| 33 |
outputs = model.generate(**inputs, max_new_tokens=100, pad_token_id=tokenizer.eos_token_id)
|
| 34 |
reply = tokenizer.decode(outputs[0], skip_special_tokens=True).split("π΄ ππ πππ:")[-1].strip()
|
| 35 |
|
| 36 |
+
# Store memory
|
| 37 |
history.append((user_input, reply))
|
| 38 |
chat_history[user_id] = history[-10:]
|
| 39 |
|
| 40 |
+
return JSONResponse({"reply": reply})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|