Spaces:
Runtime error
Runtime error
File size: 1,010 Bytes
6640248 | 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 | from fastapi import FastAPI
from pydantic import BaseModel
import requests
import os
app = FastAPI()
OPENROUTER_API_KEY = os.environ["OPENROUTER_API_KEY"]
API_URL = "https://openrouter.ai/api/v1/chat/completions"
class Prompt(BaseModel):
prompt: str
@app.post("/ai")
def ai_endpoint(data: Prompt):
headers = {
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
"HTTP-Referer": "https://huggingface.co",
"X-Title": "Roblox AI Server",
"Content-Type": "application/json"
}
payload = {
"model": "google/gemini-3-flash-preview",
"messages": [
{"role": "user", "content": data.prompt}
],
"max_tokens": 200
}
response = requests.post(API_URL, headers=headers, json=payload)
result = response.json()
if "response" in result:
return {"reply": result["response"]}
if "choices" in result:
return {"reply": result["choices"][0]["message"]["content"]}
return {"reply": str(result)}
|