Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
app = FastAPI(title="QA GPT2 API", description="Serving HuggingFace model with FastAPI")
|
| 6 |
+
|
| 7 |
+
# Load model once on startup
|
| 8 |
+
model_id = "rabiyulfahim/qa_python_gpt2" # 👈 replace with your HF repo
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 11 |
+
|
| 12 |
+
@app.get("/")
|
| 13 |
+
def home():
|
| 14 |
+
return {"message": "Welcome to QA GPT2 API 🚀"}
|
| 15 |
+
|
| 16 |
+
@app.get("/ask")
|
| 17 |
+
def ask(question: str, max_new_tokens: int = 50):
|
| 18 |
+
inputs = tokenizer(question, return_tensors="pt")
|
| 19 |
+
outputs = model.generate(**inputs, max_new_tokens=max_new_tokens)
|
| 20 |
+
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 21 |
+
return {"question": question, "answer": answer}
|