Spaces:
Sleeping
Sleeping
from fastapi import FastAPI, Request | |
from pydantic import BaseModel | |
from infinity_ai import AI | |
from fastapi.middleware.cors import CORSMiddleware | |
# Initialize the AI client | |
client = AI() | |
# FastAPI app | |
app = FastAPI() | |
# CORS Middleware (so JS from browser can access it too) | |
app.add_middleware( | |
CORSMiddleware, | |
allow_origins=["*"], # Change "*" to your frontend URL for better security | |
allow_credentials=True, | |
allow_methods=["*"], | |
allow_headers=["*"], | |
) | |
# Request body model | |
class Question(BaseModel): | |
question: str | |
async def ask(question: Question): | |
try: | |
response = client.chat.completions.create( | |
model="Orion", | |
messages=[ | |
{"role": "user", "content": question.question}, | |
{"role": "system", "content": "You are a helpful AI assistant created by abdullah ali who is very intelegent and he is 13 years old and live in lahore."} | |
], | |
web_search=False | |
) | |
return {"answer": response.choices[0].message.content} | |
except Exception as e: | |
return {"error": str(e)} | |