abdullahalioo commited on
Commit
e6b0b52
·
verified ·
1 Parent(s): 1759cbb

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +38 -0
main.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
+ from pydantic import BaseModel
3
+ from infinity_ai import AI
4
+ from fastapi.middleware.cors import CORSMiddleware
5
+
6
+ # Initialize the AI client
7
+ client = AI()
8
+
9
+ # FastAPI app
10
+ app = FastAPI()
11
+
12
+ # CORS Middleware (so JS from browser can access it too)
13
+ app.add_middleware(
14
+ CORSMiddleware,
15
+ allow_origins=["*"], # Change "*" to your frontend URL for better security
16
+ allow_credentials=True,
17
+ allow_methods=["*"],
18
+ allow_headers=["*"],
19
+ )
20
+
21
+ # Request body model
22
+ class Question(BaseModel):
23
+ question: str
24
+
25
+ @app.post("/ask")
26
+ async def ask(question: Question):
27
+ try:
28
+ response = client.chat.completions.create(
29
+ model="Orion",
30
+ messages=[
31
+ {"role": "user", "content": question.question},
32
+ {"role": "system", "content": "You are a helpful AI assistant."}
33
+ ],
34
+ web_search=False
35
+ )
36
+ return {"answer": response.choices[0].message.content}
37
+ except Exception as e:
38
+ return {"error": str(e)}