Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,37 @@ chat_history = {}
|
|
| 12 |
|
| 13 |
@app.get("/")
|
| 14 |
async def root():
|
| 15 |
-
return {"message": "🟢 API is
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
@app.get("/ai")
|
| 18 |
async def chat(request: Request):
|
|
|
|
| 12 |
|
| 13 |
@app.get("/")
|
| 14 |
async def root():
|
| 15 |
+
return {"message": "🟢 API is running! Use /ai?query=Hello&user_id=abc"}
|
| 16 |
+
|
| 17 |
+
@app.get("/ai")
|
| 18 |
+
async def chat(request: Request):
|
| 19 |
+
query_params = dict(request.query_params)
|
| 20 |
+
user_input = query_params.get("query", "")
|
| 21 |
+
user_id = query_params.get("user_id", "default")
|
| 22 |
+
|
| 23 |
+
new_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
|
| 24 |
+
user_history = chat_history.get(user_id, [])
|
| 25 |
+
bot_input_ids = torch.cat(user_history + [new_input_ids], dim=-1) if user_history else new_input_ids
|
| 26 |
+
|
| 27 |
+
output_ids = model.generate(bot_input_ids, max_new_tokens=100, pad_token_id=tokenizer.eos_token_id)
|
| 28 |
+
response = tokenizer.decode(output_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
| 29 |
+
|
| 30 |
+
chat_history[user_id] = [bot_input_ids, output_ids]
|
| 31 |
+
return JSONResponse({"reply": response})from fastapi import FastAPI, Request
|
| 32 |
+
from fastapi.responses import JSONResponse
|
| 33 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 34 |
+
import torch
|
| 35 |
+
|
| 36 |
+
app = FastAPI()
|
| 37 |
+
|
| 38 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
| 39 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
| 40 |
+
|
| 41 |
+
chat_history = {}
|
| 42 |
+
|
| 43 |
+
@app.get("/")
|
| 44 |
+
async def root():
|
| 45 |
+
return {"message": "🟢 API is running! Use /ai?query=Hello&user_id=abc"}
|
| 46 |
|
| 47 |
@app.get("/ai")
|
| 48 |
async def chat(request: Request):
|