Spaces:
Build error
Build error
Upload 3 files
Browse files- Dockerfile +37 -38
- main.py +4 -31
- requirements.txt +1 -1
Dockerfile
CHANGED
|
@@ -1,38 +1,37 @@
|
|
| 1 |
-
# Use Ubuntu as the base image
|
| 2 |
-
FROM ubuntu:22.04
|
| 3 |
-
|
| 4 |
-
# Set the working directory in the container
|
| 5 |
-
WORKDIR /app
|
| 6 |
-
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
#
|
| 18 |
-
RUN
|
| 19 |
-
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
CMD ["sh", "-c", "ollama serve & sleep 5 && ollama pull llama3 && uvicorn main:app --host 0.0.0.0 --port 8000"]
|
|
|
|
| 1 |
+
# Use Ubuntu as the base image
|
| 2 |
+
FROM ubuntu:22.04
|
| 3 |
+
|
| 4 |
+
# Set the working directory in the container
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Install system dependencies and Python
|
| 8 |
+
RUN apt-get update && apt-get install -y \
|
| 9 |
+
python3 \
|
| 10 |
+
python3-pip \
|
| 11 |
+
curl \
|
| 12 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 13 |
+
|
| 14 |
+
# Set Python3 as the default
|
| 15 |
+
RUN ln -s /usr/bin/python3 /usr/bin/python
|
| 16 |
+
|
| 17 |
+
# Install Ollama
|
| 18 |
+
RUN curl -fsSL https://ollama.com/install.sh | bash
|
| 19 |
+
|
| 20 |
+
# Ensure Ollama is in the system path
|
| 21 |
+
ENV PATH="/root/.ollama/bin:$PATH"
|
| 22 |
+
|
| 23 |
+
# Pre-download the Llama3 model to avoid downloading it at runtime
|
| 24 |
+
RUN ollama serve & sleep 5 && ollama pull llama3
|
| 25 |
+
|
| 26 |
+
# Copy the requirements file and install dependencies
|
| 27 |
+
COPY requirements.txt ./
|
| 28 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 29 |
+
|
| 30 |
+
# Copy the application files
|
| 31 |
+
COPY . .
|
| 32 |
+
|
| 33 |
+
# Expose the FastAPI default port
|
| 34 |
+
EXPOSE 8000
|
| 35 |
+
|
| 36 |
+
# Start Ollama and FastAPI
|
| 37 |
+
CMD ["sh", "-c", "ollama serve & uvicorn main:app --host 0.0.0.0 --port 8000"]
|
|
|
main.py
CHANGED
|
@@ -1,37 +1,14 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from langchain_community.llms import Ollama # Correct Import
|
| 4 |
-
import os
|
| 5 |
import logging
|
| 6 |
import time # Import time module
|
| 7 |
-
from dotenv import load_dotenv
|
| 8 |
-
|
| 9 |
-
# Load environment variables
|
| 10 |
-
load_dotenv()
|
| 11 |
|
| 12 |
# Configure logging
|
| 13 |
logging.basicConfig(level=logging.INFO)
|
| 14 |
|
| 15 |
-
# API keys from .env
|
| 16 |
-
API_KEYS = {
|
| 17 |
-
"user1": os.getenv("API_KEY_USER1"),
|
| 18 |
-
"user2": os.getenv("API_KEY_USER2"),
|
| 19 |
-
}
|
| 20 |
-
|
| 21 |
app = FastAPI()
|
| 22 |
|
| 23 |
-
# API Key Authentication
|
| 24 |
-
def verify_api_key(request: Request, api_key: str = Header(None, alias="X-API-Key")):
|
| 25 |
-
logging.info(f"Received Headers: {request.headers}") # Log headers
|
| 26 |
-
if not api_key:
|
| 27 |
-
raise HTTPException(status_code=401, detail="API key is missing")
|
| 28 |
-
|
| 29 |
-
api_key = api_key.strip()
|
| 30 |
-
if api_key not in API_KEYS.values():
|
| 31 |
-
raise HTTPException(status_code=401, detail="Invalid API key")
|
| 32 |
-
|
| 33 |
-
return api_key
|
| 34 |
-
|
| 35 |
# OpenAI-compatible request format
|
| 36 |
class OpenAIRequest(BaseModel):
|
| 37 |
model: str
|
|
@@ -42,12 +19,8 @@ class OpenAIRequest(BaseModel):
|
|
| 42 |
def get_llm(model_name: str):
|
| 43 |
return Ollama(model=model_name)
|
| 44 |
|
| 45 |
-
@app.get("/")
|
| 46 |
-
def home():
|
| 47 |
-
return {"message": "OpenAI-compatible LangChain + Ollama API is running"}
|
| 48 |
-
|
| 49 |
@app.post("/v1/chat/completions")
|
| 50 |
-
def generate_text(request: OpenAIRequest
|
| 51 |
try:
|
| 52 |
llm = get_llm(request.model)
|
| 53 |
|
|
@@ -62,7 +35,7 @@ def generate_text(request: OpenAIRequest, api_key: str = Depends(verify_api_key)
|
|
| 62 |
response = {
|
| 63 |
"id": "chatcmpl-123",
|
| 64 |
"object": "chat.completion",
|
| 65 |
-
"created": int(time.time()),
|
| 66 |
"model": request.model,
|
| 67 |
"choices": [
|
| 68 |
{
|
|
@@ -82,4 +55,4 @@ def generate_text(request: OpenAIRequest, api_key: str = Depends(verify_api_key)
|
|
| 82 |
|
| 83 |
except Exception as e:
|
| 84 |
logging.error(f"Error generating response: {e}")
|
| 85 |
-
raise HTTPException(status_code=500, detail="Internal server error")
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from langchain_community.llms import Ollama # Correct Import
|
|
|
|
| 4 |
import logging
|
| 5 |
import time # Import time module
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Configure logging
|
| 8 |
logging.basicConfig(level=logging.INFO)
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
app = FastAPI()
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# OpenAI-compatible request format
|
| 13 |
class OpenAIRequest(BaseModel):
|
| 14 |
model: str
|
|
|
|
| 19 |
def get_llm(model_name: str):
|
| 20 |
return Ollama(model=model_name)
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
@app.post("/v1/chat/completions")
|
| 23 |
+
def generate_text(request: OpenAIRequest):
|
| 24 |
try:
|
| 25 |
llm = get_llm(request.model)
|
| 26 |
|
|
|
|
| 35 |
response = {
|
| 36 |
"id": "chatcmpl-123",
|
| 37 |
"object": "chat.completion",
|
| 38 |
+
"created": int(time.time()),
|
| 39 |
"model": request.model,
|
| 40 |
"choices": [
|
| 41 |
{
|
|
|
|
| 55 |
|
| 56 |
except Exception as e:
|
| 57 |
logging.error(f"Error generating response: {e}")
|
| 58 |
+
raise HTTPException(status_code=500, detail="Internal server error")
|
requirements.txt
CHANGED
|
@@ -5,4 +5,4 @@ langchain
|
|
| 5 |
requests
|
| 6 |
langchain_community
|
| 7 |
python-dotenv
|
| 8 |
-
cloudflared
|
|
|
|
| 5 |
requests
|
| 6 |
langchain_community
|
| 7 |
python-dotenv
|
| 8 |
+
cloudflared
|