Spaces:
Running
Running
File size: 2,659 Bytes
28e75a0 197881b 28e75a0 048b640 28e75a0 562ff6d 4861fe6 562ff6d 4861fe6 28e75a0 197881b 562ff6d 197881b 3265323 197881b 4861fe6 197881b 28e75a0 562ff6d 3878793 562ff6d 3878793 562ff6d 28e75a0 197881b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | import os
import time
from fastapi import FastAPI, HTTPException, Header, Depends, Request
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
app = FastAPI()
SECRET_API_KEY = os.getenv("API_PASSWORD")
model_id = "Qwen/Qwen2.5-3B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float32)
@app.get("/health")
def health_check():
return {"status": "active"}
@app.get("/")
def home_endpoint():
return {"status": "Server is running perfectly!"}
# n8n OpenAI মডেলে ডিফল্টভাবে "Bearer " যুক্ত করে পাসওয়ার্ড পাঠায়
def verify_api_key(authorization: str = Header(None)):
if not SECRET_API_KEY:
raise HTTPException(status_code=500, detail="API Key not configured in Space Secrets")
expected_auth = f"Bearer {SECRET_API_KEY}"
if authorization != expected_auth:
raise HTTPException(status_code=401, detail="Unauthorized: Invalid API Key")
# n8n-এর AI Agent ঠিক এই লিংকেই রিকোয়েস্ট পাঠাবে
@app.post("/chat/completions", dependencies=[Depends(verify_api_key)])
@app.post("/v1/chat/completions", dependencies=[Depends(verify_api_key)])
async def chat_completions(request: Request):
data = await request.json()
# n8n নিজে থেকেই সিস্টেম প্রম্পট, পোস্টগ্রেসের মেমোরি এবং নতুন মেসেজ এই 'messages'-এর ভেতর পাঠিয়ে দেবে
messages = data.get("messages", [])
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = tokenizer([text], return_tensors="pt")
outputs = model.generate(
**inputs,
max_new_tokens=150,
do_sample=True,
temperature=0.6,
top_p=0.85,
num_beams=1,
pad_token_id=tokenizer.eos_token_id
)
response_text = tokenizer.decode(outputs[0][inputs.input_ids.shape[-1]:], skip_special_tokens=True)
# n8n-কে ঠিক OpenAI-এর ফরম্যাটেই উত্তর ফেরত দিতে হবে
return {
"id": f"chatcmpl-{int(time.time())}",
"object": "chat.completion",
"created": int(time.time()),
"model": model_id,
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": response_text,
},
"finish_reason": "stop"
}]
} |