Update app.py
Browse files
app.py
CHANGED
|
@@ -5,14 +5,16 @@ import torch
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
|
|
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
| 9 |
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
| 10 |
|
|
|
|
| 11 |
chat_history = {}
|
| 12 |
|
| 13 |
@app.get("/")
|
| 14 |
async def root():
|
| 15 |
-
return {"message": "🟢 API is running
|
| 16 |
|
| 17 |
@app.get("/ai")
|
| 18 |
async def chat(request: Request):
|
|
@@ -20,35 +22,8 @@ async def chat(request: Request):
|
|
| 20 |
user_input = query_params.get("query", "")
|
| 21 |
user_id = query_params.get("user_id", "default")
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 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):
|
| 49 |
-
query_params = dict(request.query_params)
|
| 50 |
-
user_input = query_params.get("query", "")
|
| 51 |
-
user_id = query_params.get("user_id", "default")
|
| 52 |
|
| 53 |
new_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
|
| 54 |
user_history = chat_history.get(user_id, [])
|
|
@@ -58,4 +33,9 @@ async def chat(request: Request):
|
|
| 58 |
response = tokenizer.decode(output_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
| 59 |
|
| 60 |
chat_history[user_id] = [bot_input_ids, output_ids]
|
| 61 |
-
return JSONResponse({"reply": response})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
+
# Load model and tokenizer
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
| 10 |
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
| 11 |
|
| 12 |
+
# In-memory history per user
|
| 13 |
chat_history = {}
|
| 14 |
|
| 15 |
@app.get("/")
|
| 16 |
async def root():
|
| 17 |
+
return {"message": "🟢 API is running. Use /ai?query=Hello&user_id=yourname"}
|
| 18 |
|
| 19 |
@app.get("/ai")
|
| 20 |
async def chat(request: Request):
|
|
|
|
| 22 |
user_input = query_params.get("query", "")
|
| 23 |
user_id = query_params.get("user_id", "default")
|
| 24 |
|
| 25 |
+
if not user_input:
|
| 26 |
+
return JSONResponse({"error": "Missing 'query' parameter"}, status_code=400)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
new_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
|
| 29 |
user_history = chat_history.get(user_id, [])
|
|
|
|
| 33 |
response = tokenizer.decode(output_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
| 34 |
|
| 35 |
chat_history[user_id] = [bot_input_ids, output_ids]
|
| 36 |
+
return JSONResponse({"reply": response})
|
| 37 |
+
|
| 38 |
+
# Only needed if running locally, not in Hugging Face Space
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
import uvicorn
|
| 41 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|