Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
-
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
|
| 8 |
|
| 9 |
-
# 🔱
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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://
|
| 20 |
"Content-Type": "application/json"
|
| 21 |
}
|
|
|
|
| 22 |
payload = {
|
| 23 |
"model": MODEL,
|
| 24 |
-
"messages": [
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
}
|
| 26 |
-
|
|
|
|
| 27 |
return response.json()
|
|
|
|
| 28 |
except Exception as e:
|
| 29 |
-
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import uvicorn
|
| 4 |
from fastapi import FastAPI, Request
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
+
from fastapi.responses import HTMLResponse
|
| 7 |
+
from fastapi.staticfiles import StaticFiles
|
| 8 |
|
| 9 |
app = FastAPI()
|
|
|
|
| 10 |
|
| 11 |
+
# 🔱 CORS Policy "Unleashed" - බ්රවුසරයේ ඇති සියලුම බාධක ඉවත් කරයි
|
| 12 |
+
app.add_middleware(
|
| 13 |
+
CORSMiddleware,
|
| 14 |
+
allow_origins=["*"],
|
| 15 |
+
allow_credentials=True,
|
| 16 |
+
allow_methods=["*"],
|
| 17 |
+
allow_headers=["*"],
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# 🔱 Configuration
|
| 21 |
+
# Hugging Face Secrets වලින් Key එක ලබා ගනී (Invisible to public)
|
| 22 |
API_KEY = os.getenv("OPENROUTER_KEY")
|
| 23 |
MODEL = "tencent/hy3-preview:free"
|
| 24 |
+
OPENROUTER_URL = "https://openrouter.ai/api/v1/chat/completions"
|
| 25 |
|
| 26 |
@app.post("/generate")
|
| 27 |
async def generate(request: Request):
|
| 28 |
try:
|
| 29 |
data = await request.json()
|
| 30 |
+
user_prompt = data.get("prompt")
|
| 31 |
+
|
| 32 |
headers = {
|
| 33 |
"Authorization": f"Bearer {API_KEY}",
|
| 34 |
+
"HTTP-Referer": "https://huggingface.co/spaces",
|
| 35 |
"Content-Type": "application/json"
|
| 36 |
}
|
| 37 |
+
|
| 38 |
payload = {
|
| 39 |
"model": MODEL,
|
| 40 |
+
"messages": [
|
| 41 |
+
{"role": "system", "content": "You are Elephant AI Pro, a master level coding assistant built by MINZO-PRIME. Your logic is powered by Tencent Hy3."},
|
| 42 |
+
{"role": "user", "content": user_prompt}
|
| 43 |
+
],
|
| 44 |
+
"temperature": 0.7
|
| 45 |
}
|
| 46 |
+
|
| 47 |
+
response = requests.post(OPENROUTER_URL, headers=headers, json=payload, timeout=60)
|
| 48 |
return response.json()
|
| 49 |
+
|
| 50 |
except Exception as e:
|
| 51 |
+
return {"error": str(e)}
|
| 52 |
+
|
| 53 |
+
# 🔱 Main Page Loader
|
| 54 |
+
@app.get("/", response_class=HTMLResponse)
|
| 55 |
+
async def read_index():
|
| 56 |
+
with open("index.html", "r", encoding="utf-8") as f:
|
| 57 |
+
return f.read()
|
| 58 |
+
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|