abhojulius commited on
Commit
cbb2c2d
·
verified ·
1 Parent(s): 424794f

abhojulius@gmail.com

Browse files
Files changed (5) hide show
  1. Dockerfile +12 -0
  2. app/auth.py +4 -0
  3. app/main.py +16 -0
  4. app/model.py +10 -0
  5. requirements.txt +4 -0
Dockerfile ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ RUN apt-get update && apt-get install -y git && rm -rf /var/lib/apt/lists/*
4
+
5
+ WORKDIR /app
6
+
7
+ COPY requirements.txt .
8
+ RUN pip install --no-cache-dir -r requirements.txt
9
+
10
+ COPY app/ /app/
11
+
12
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
app/auth.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ VALID_KEYS = {"test123", "coach001", "demo-key"}
2
+
3
+ def validate_api_key(key: str) -> bool:
4
+ return key in VALID_KEYS
app/main.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request, HTTPException
2
+ from auth import validate_api_key
3
+ from model import generate_response
4
+
5
+ app = FastAPI()
6
+
7
+ @app.post("/chat")
8
+ async def chat(request: Request):
9
+ body = await request.json()
10
+ api_key = request.headers.get("X-API-Key")
11
+ if not validate_api_key(api_key):
12
+ raise HTTPException(status_code=401, detail="Invalid API Key")
13
+
14
+ user_input = body.get("message")
15
+ response = generate_response(user_input)
16
+ return {"response": response}
app/model.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import os
3
+ openai.api_key = os.getenv("OPENAI_API_KEY")
4
+
5
+ def generate_response(prompt: str) -> str:
6
+ completion = openai.ChatCompletion.create(
7
+ model="gpt-4",
8
+ messages=[{"role": "user", "content": prompt}]
9
+ )
10
+ return completion.choices[0].message.content.strip()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ openai
4
+ python-dotenv