Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,29 @@
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
import requests
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI()
|
|
|
|
| 6 |
|
| 7 |
-
# 🔱
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
allow_origins=["*"],
|
| 11 |
-
allow_methods=["*"],
|
| 12 |
-
allow_headers=["*"],
|
| 13 |
-
)
|
| 14 |
-
|
| 15 |
-
API_URL = "https://api.us-west-2.modal.direct/v1/chat/completions"
|
| 16 |
-
API_KEY = "modalresearch_0yKf_z9azDFpOXr-W1N1WsmBUgXf6Ux-yvpVsFQWzks"
|
| 17 |
|
| 18 |
@app.post("/generate")
|
| 19 |
async def generate(request: Request):
|
| 20 |
try:
|
| 21 |
data = await request.json()
|
| 22 |
-
headers = {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
payload = {
|
| 24 |
-
"model":
|
| 25 |
-
"messages": [{"role": "user", "content": data.get("prompt")}]
|
| 26 |
-
"max_tokens": 1024
|
| 27 |
}
|
| 28 |
-
|
| 29 |
-
response = requests.post(API_URL, headers=headers, json=payload)
|
| 30 |
return response.json()
|
| 31 |
except Exception as e:
|
| 32 |
return {"error": str(e)}
|
|
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
import requests
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
+
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
|
| 8 |
|
| 9 |
+
# 🔱 Secrets වලින් Key එක ලබාගැනීම (Invisible to View Source)
|
| 10 |
+
API_KEY = os.getenv("OPENROUTER_KEY")
|
| 11 |
+
MODEL = "tencent/hy3-preview:free"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
@app.post("/generate")
|
| 14 |
async def generate(request: Request):
|
| 15 |
try:
|
| 16 |
data = await request.json()
|
| 17 |
+
headers = {
|
| 18 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 19 |
+
"HTTP-Referer": "https://hf.space",
|
| 20 |
+
"Content-Type": "application/json"
|
| 21 |
+
}
|
| 22 |
payload = {
|
| 23 |
+
"model": MODEL,
|
| 24 |
+
"messages": [{"role": "user", "content": data.get("prompt")}]
|
|
|
|
| 25 |
}
|
| 26 |
+
response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
|
|
|
|
| 27 |
return response.json()
|
| 28 |
except Exception as e:
|
| 29 |
return {"error": str(e)}
|