Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
-
from fastapi.responses import JSONResponse
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
import requests
|
| 5 |
import json
|
|
@@ -10,28 +9,16 @@ import uvicorn
|
|
| 10 |
TOKEN = os.getenv("POLLINATIONS_TOKEN")
|
| 11 |
POLLINATIONS_URL = "https://text.pollinations.ai/openai"
|
| 12 |
|
| 13 |
-
app = FastAPI(
|
| 14 |
-
title="Pollinations OpenAI API",
|
| 15 |
-
description="OpenAI Compatible API powered by Pollinations AI",
|
| 16 |
-
version="1.0.0"
|
| 17 |
-
)
|
| 18 |
|
| 19 |
-
# CORS
|
| 20 |
app.add_middleware(
|
| 21 |
CORSMiddleware,
|
| 22 |
allow_origins=["*"],
|
| 23 |
-
allow_credentials=True,
|
| 24 |
allow_methods=["*"],
|
| 25 |
allow_headers=["*"],
|
| 26 |
)
|
| 27 |
|
| 28 |
-
# Available models
|
| 29 |
-
MODELS = [
|
| 30 |
-
"openai", "openai-large", "openai-reasoning", "qwen-coder", "llama",
|
| 31 |
-
"llamascout", "mistral", "unity", "gemini", "phi", "deepseek",
|
| 32 |
-
"deepseek-reasoning", "rtist", "searchgpt", "evil", "openai-audio"
|
| 33 |
-
]
|
| 34 |
-
|
| 35 |
@app.get("/")
|
| 36 |
async def root():
|
| 37 |
return {
|
|
@@ -39,85 +26,49 @@ async def root():
|
|
| 39 |
"status": "running",
|
| 40 |
"endpoints": {
|
| 41 |
"models": "/models",
|
| 42 |
-
"
|
| 43 |
-
"openai_compatible": "/v1/chat/completions"
|
| 44 |
-
},
|
| 45 |
-
"usage": {
|
| 46 |
-
"models": "GET /models",
|
| 47 |
-
"chat": "POST /chat/completions",
|
| 48 |
-
"openai": "POST /v1/chat/completions"
|
| 49 |
}
|
| 50 |
}
|
| 51 |
|
| 52 |
-
@app.get("/health")
|
| 53 |
-
async def health():
|
| 54 |
-
return {"status": "healthy"}
|
| 55 |
-
|
| 56 |
@app.get("/models")
|
| 57 |
async def get_models():
|
| 58 |
-
"""
|
| 59 |
return {
|
| 60 |
"object": "list",
|
| 61 |
"data": [
|
| 62 |
-
{
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
}
|
| 68 |
-
for model in MODELS
|
| 69 |
]
|
| 70 |
}
|
| 71 |
|
| 72 |
@app.post("/chat/completions")
|
| 73 |
-
@app.post("/v1/chat/completions")
|
| 74 |
async def chat_completions(data: dict):
|
| 75 |
-
"""
|
| 76 |
-
|
| 77 |
model = data.get("model", "openai")
|
| 78 |
messages = data.get("messages", [])
|
| 79 |
temperature = data.get("temperature", 0.7)
|
| 80 |
-
max_tokens = data.get("max_tokens", 1000)
|
| 81 |
-
stream = data.get("stream", False)
|
| 82 |
|
| 83 |
-
if not messages:
|
| 84 |
-
return JSONResponse(
|
| 85 |
-
status_code=400,
|
| 86 |
-
content={"error": {"message": "messages field is required"}}
|
| 87 |
-
)
|
| 88 |
-
|
| 89 |
-
# Prepare payload for Pollinations
|
| 90 |
payload = {
|
| 91 |
"model": model,
|
| 92 |
"messages": messages,
|
| 93 |
"temperature": temperature,
|
| 94 |
-
"
|
| 95 |
-
"referrer": "hf-openai-api"
|
| 96 |
}
|
| 97 |
|
| 98 |
-
if stream:
|
| 99 |
-
payload["stream"] = True
|
| 100 |
-
|
| 101 |
-
# Headers
|
| 102 |
headers = {"Content-Type": "application/json"}
|
| 103 |
if TOKEN:
|
| 104 |
headers["Authorization"] = f"Bearer {TOKEN}"
|
| 105 |
|
| 106 |
try:
|
| 107 |
-
response = requests.post(
|
| 108 |
-
POLLINATIONS_URL,
|
| 109 |
-
json=payload,
|
| 110 |
-
headers=headers,
|
| 111 |
-
timeout=120
|
| 112 |
-
)
|
| 113 |
-
response.raise_for_status()
|
| 114 |
result = response.json()
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
"id": f"chatcmpl-{hash(str(payload)) % 10000000}",
|
| 119 |
"object": "chat.completion",
|
| 120 |
-
"created": 1677858242,
|
| 121 |
"model": model,
|
| 122 |
"choices": [{
|
| 123 |
"index": 0,
|
|
@@ -126,27 +77,10 @@ async def chat_completions(data: dict):
|
|
| 126 |
"content": result["choices"][0]["message"]["content"]
|
| 127 |
},
|
| 128 |
"finish_reason": "stop"
|
| 129 |
-
}]
|
| 130 |
-
"usage": {
|
| 131 |
-
"prompt_tokens": len(str(messages)),
|
| 132 |
-
"completion_tokens": len(result["choices"][0]["message"]["content"]),
|
| 133 |
-
"total_tokens": len(str(messages)) + len(result["choices"][0]["message"]["content"])
|
| 134 |
-
}
|
| 135 |
}
|
| 136 |
-
|
| 137 |
-
return openai_response
|
| 138 |
-
|
| 139 |
-
except requests.exceptions.RequestException as e:
|
| 140 |
-
return JSONResponse(
|
| 141 |
-
status_code=500,
|
| 142 |
-
content={"error": {"message": f"Pollinations API error: {str(e)}"}}
|
| 143 |
-
)
|
| 144 |
except Exception as e:
|
| 145 |
-
return
|
| 146 |
-
status_code=500,
|
| 147 |
-
content={"error": {"message": f"Server error: {str(e)}"}}
|
| 148 |
-
)
|
| 149 |
|
| 150 |
if __name__ == "__main__":
|
| 151 |
-
|
| 152 |
-
uvicorn.run(app, host="0.0.0.0", port=port)
|
|
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
import requests
|
| 4 |
import json
|
|
|
|
| 9 |
TOKEN = os.getenv("POLLINATIONS_TOKEN")
|
| 10 |
POLLINATIONS_URL = "https://text.pollinations.ai/openai"
|
| 11 |
|
| 12 |
+
app = FastAPI(title="Pollinations OpenAI API")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# CORS
|
| 15 |
app.add_middleware(
|
| 16 |
CORSMiddleware,
|
| 17 |
allow_origins=["*"],
|
|
|
|
| 18 |
allow_methods=["*"],
|
| 19 |
allow_headers=["*"],
|
| 20 |
)
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
@app.get("/")
|
| 23 |
async def root():
|
| 24 |
return {
|
|
|
|
| 26 |
"status": "running",
|
| 27 |
"endpoints": {
|
| 28 |
"models": "/models",
|
| 29 |
+
"chat": "/chat/completions"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
}
|
| 31 |
}
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
@app.get("/models")
|
| 34 |
async def get_models():
|
| 35 |
+
"""Models endpoint - यहीं missing था!"""
|
| 36 |
return {
|
| 37 |
"object": "list",
|
| 38 |
"data": [
|
| 39 |
+
{"id": "openai", "object": "model", "owned_by": "pollinations"},
|
| 40 |
+
{"id": "llama", "object": "model", "owned_by": "pollinations"},
|
| 41 |
+
{"id": "mistral", "object": "model", "owned_by": "pollinations"},
|
| 42 |
+
{"id": "gemini", "object": "model", "owned_by": "pollinations"},
|
| 43 |
+
{"id": "deepseek", "object": "model", "owned_by": "pollinations"}
|
|
|
|
|
|
|
| 44 |
]
|
| 45 |
}
|
| 46 |
|
| 47 |
@app.post("/chat/completions")
|
|
|
|
| 48 |
async def chat_completions(data: dict):
|
| 49 |
+
"""Chat endpoint"""
|
|
|
|
| 50 |
model = data.get("model", "openai")
|
| 51 |
messages = data.get("messages", [])
|
| 52 |
temperature = data.get("temperature", 0.7)
|
|
|
|
|
|
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
payload = {
|
| 55 |
"model": model,
|
| 56 |
"messages": messages,
|
| 57 |
"temperature": temperature,
|
| 58 |
+
"referrer": "hf-api"
|
|
|
|
| 59 |
}
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
headers = {"Content-Type": "application/json"}
|
| 62 |
if TOKEN:
|
| 63 |
headers["Authorization"] = f"Bearer {TOKEN}"
|
| 64 |
|
| 65 |
try:
|
| 66 |
+
response = requests.post(POLLINATIONS_URL, json=payload, headers=headers, timeout=60)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
result = response.json()
|
| 68 |
|
| 69 |
+
return {
|
| 70 |
+
"id": "chatcmpl-123",
|
|
|
|
| 71 |
"object": "chat.completion",
|
|
|
|
| 72 |
"model": model,
|
| 73 |
"choices": [{
|
| 74 |
"index": 0,
|
|
|
|
| 77 |
"content": result["choices"][0]["message"]["content"]
|
| 78 |
},
|
| 79 |
"finish_reason": "stop"
|
| 80 |
+
}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
except Exception as e:
|
| 83 |
+
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
if __name__ == "__main__":
|
| 86 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|