|
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM |
|
from fastapi import FastAPI |
|
|
|
|
|
MODEL_ID = "MBZUAI/LaMini-Flan-T5-77M" |
|
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) |
|
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID) |
|
|
|
app = FastAPI() |
|
|
|
@app.get("/") |
|
def root(): |
|
return { |
|
"message": "✅ LaMini-Flan-T5-77M Chatbot is running!", |
|
"usage": "Send GET /chat?query=your+question" |
|
} |
|
|
|
@app.get("/chat") |
|
def chat(query: str): |
|
""" |
|
Example: GET /chat?query=What+is+Python%3F |
|
Returns JSON: {"answer": "...model’s reply..."} |
|
""" |
|
|
|
inputs = tokenizer(query, return_tensors="pt") |
|
|
|
outputs = model.generate(**inputs, max_new_tokens=100) |
|
|
|
response = tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
return {"answer": response.strip()} |