Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
CHANGED
|
@@ -1,104 +1 @@
|
|
| 1 |
-
|
| 2 |
-
from fastapi.responses import StreamingResponse
|
| 3 |
-
from pydantic import BaseModel
|
| 4 |
-
from typing import List
|
| 5 |
-
from g4f import ChatCompletion
|
| 6 |
-
from slowapi import Limiter
|
| 7 |
-
from slowapi.util import get_remote_address
|
| 8 |
-
|
| 9 |
-
app = FastAPI()
|
| 10 |
-
|
| 11 |
-
# Initialize the rate limiter
|
| 12 |
-
limiter = Limiter(key_func=get_remote_address)
|
| 13 |
-
|
| 14 |
-
# List of available models
|
| 15 |
-
models = [
|
| 16 |
-
"gpt-4o", "gpt-4o-mini", "gpt-4",
|
| 17 |
-
"gpt-4-turbo", "gpt-3.5-turbo",
|
| 18 |
-
"claude-3.7-sonnet", "o3-mini", "o1", "claude-3.5", "llama-3.1-405b", "gemini-flash", "blackboxai-pro", "openchat-3.5", "glm-4-9B", "blackboxai"
|
| 19 |
-
]
|
| 20 |
-
|
| 21 |
-
# Request model
|
| 22 |
-
class Message(BaseModel):
|
| 23 |
-
role: str
|
| 24 |
-
content: str
|
| 25 |
-
|
| 26 |
-
class ChatRequest(BaseModel):
|
| 27 |
-
model: str
|
| 28 |
-
messages: List[Message]
|
| 29 |
-
streaming: bool = True # Add streaming support
|
| 30 |
-
|
| 31 |
-
class ChatResponse(BaseModel):
|
| 32 |
-
role: str
|
| 33 |
-
content: str
|
| 34 |
-
|
| 35 |
-
# Dependency to check API key
|
| 36 |
-
async def verify_api_key(x_api_key: str = Header(...)):
|
| 37 |
-
if x_api_key != "your-api-key": # Replace with your actual API key
|
| 38 |
-
raise HTTPException(status_code=403, detail="Invalid API key")
|
| 39 |
-
|
| 40 |
-
@app.get("/v1/models", tags=["Models"])
|
| 41 |
-
async def get_models():
|
| 42 |
-
"""Endpoint to get the list of available models."""
|
| 43 |
-
return {"models": models}
|
| 44 |
-
|
| 45 |
-
@app.post("/v1/chat/completions", tags=["Chat Completion"])
|
| 46 |
-
@limiter.limit("10/minute") # Rate limit to 10 requests per minute
|
| 47 |
-
async def chat_completion(
|
| 48 |
-
request: Request,
|
| 49 |
-
chat_request: ChatRequest,
|
| 50 |
-
api_key: str = Depends(verify_api_key)
|
| 51 |
-
):
|
| 52 |
-
# Validate model
|
| 53 |
-
if chat_request.model not in models:
|
| 54 |
-
raise HTTPException(status_code=400, detail="Invalid model selected.")
|
| 55 |
-
|
| 56 |
-
# Check if messages are provided
|
| 57 |
-
if not chat_request.messages:
|
| 58 |
-
raise HTTPException(status_code=400, detail="Messages cannot be empty.")
|
| 59 |
-
|
| 60 |
-
# Convert messages to the format expected by ChatCompletion
|
| 61 |
-
formatted_messages = [{"role": msg.role, "content": msg.content} for msg in chat_request.messages]
|
| 62 |
-
|
| 63 |
-
try:
|
| 64 |
-
if chat_request.streaming:
|
| 65 |
-
# Stream the response
|
| 66 |
-
def event_stream():
|
| 67 |
-
response = ChatCompletion.create(
|
| 68 |
-
model=chat_request.model,
|
| 69 |
-
messages=formatted_messages,
|
| 70 |
-
stream=True # Enable streaming
|
| 71 |
-
)
|
| 72 |
-
|
| 73 |
-
for chunk in response:
|
| 74 |
-
if isinstance(chunk, dict) and 'choices' in chunk:
|
| 75 |
-
for choice in chunk['choices']:
|
| 76 |
-
if 'message' in choice:
|
| 77 |
-
yield f"data: {choice['message']['content']}\n\n"
|
| 78 |
-
else:
|
| 79 |
-
yield f"data: {chunk}\n\n" # Fallback if chunk is not as expected
|
| 80 |
-
|
| 81 |
-
return StreamingResponse(event_stream(), media_type="text/event-stream")
|
| 82 |
-
else:
|
| 83 |
-
# Non-streaming response
|
| 84 |
-
response = ChatCompletion.create(
|
| 85 |
-
model=chat_request.model,
|
| 86 |
-
messages=formatted_messages
|
| 87 |
-
)
|
| 88 |
-
|
| 89 |
-
if isinstance(response, str):
|
| 90 |
-
response_content = response # Directly use if it's a string
|
| 91 |
-
else:
|
| 92 |
-
try:
|
| 93 |
-
response_content = response['choices'][0]['message']['content']
|
| 94 |
-
except (IndexError, KeyError):
|
| 95 |
-
raise HTTPException(status_code=500, detail="Unexpected response structure.")
|
| 96 |
-
|
| 97 |
-
return ChatResponse(role="assistant", content=response_content)
|
| 98 |
-
|
| 99 |
-
except Exception as e:
|
| 100 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 101 |
-
|
| 102 |
-
if __name__ == "__main__":
|
| 103 |
-
import uvicorn
|
| 104 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
+
import module
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|