Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,25 +3,28 @@ import requests
|
|
| 3 |
import uvicorn
|
| 4 |
from fastapi import FastAPI, Request
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
-
from
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
@app.post("/generate")
|
| 27 |
async def generate(request: Request):
|
|
@@ -29,32 +32,32 @@ async def generate(request: Request):
|
|
| 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
|
| 35 |
"Content-Type": "application/json"
|
| 36 |
}
|
| 37 |
|
| 38 |
payload = {
|
| 39 |
"model": MODEL,
|
| 40 |
"messages": [
|
| 41 |
-
{
|
|
|
|
|
|
|
|
|
|
| 42 |
{"role": "user", "content": user_prompt}
|
| 43 |
-
]
|
| 44 |
-
"temperature": 0.7
|
| 45 |
}
|
| 46 |
|
| 47 |
-
response = requests.post(
|
| 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)
|
|
|
|
| 3 |
import uvicorn
|
| 4 |
from fastapi import FastAPI, Request
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
+
from duckduckgo_search import DDGS
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
|
|
|
| 10 |
app.add_middleware(
|
| 11 |
CORSMiddleware,
|
| 12 |
+
allow_origins=["*"],
|
|
|
|
| 13 |
allow_methods=["*"],
|
| 14 |
allow_headers=["*"],
|
| 15 |
)
|
| 16 |
|
|
|
|
|
|
|
| 17 |
API_KEY = os.getenv("OPENROUTER_KEY")
|
| 18 |
MODEL = "tencent/hy3-preview:free"
|
| 19 |
+
|
| 20 |
+
def get_web_info(query):
|
| 21 |
+
"""DuckDuckGo හරහා නොමිලේ අන්තර්ජාලය පීරීම"""
|
| 22 |
+
try:
|
| 23 |
+
with DDGS() as ddgs:
|
| 24 |
+
results = [r['body'] for r in ddgs.text(query, max_results=3)]
|
| 25 |
+
return "\n".join(results)
|
| 26 |
+
except Exception:
|
| 27 |
+
return "No real-time info found."
|
| 28 |
|
| 29 |
@app.post("/generate")
|
| 30 |
async def generate(request: Request):
|
|
|
|
| 32 |
data = await request.json()
|
| 33 |
user_prompt = data.get("prompt")
|
| 34 |
|
| 35 |
+
# 🔱 පියවර 1: අන්තර්ජාලයෙන් තොරතුරු ලබා ගැනීම
|
| 36 |
+
web_context = get_web_info(user_prompt)
|
| 37 |
+
|
| 38 |
+
# 🔱 පියවර 2: මොඩලයට ලබා දෙන උපදෙස් (Hybrid System Prompt)
|
| 39 |
headers = {
|
| 40 |
"Authorization": f"Bearer {API_KEY}",
|
| 41 |
+
"HTTP-Referer": "https://huggingface.co",
|
| 42 |
"Content-Type": "application/json"
|
| 43 |
}
|
| 44 |
|
| 45 |
payload = {
|
| 46 |
"model": MODEL,
|
| 47 |
"messages": [
|
| 48 |
+
{
|
| 49 |
+
"role": "system",
|
| 50 |
+
"content": f"You are Elephant AI Pro with DuckDuckGo Search. Current Web Info: {web_context}. Use this info to provide accurate, real-time answers. Master: MINZO-PRIME."
|
| 51 |
+
},
|
| 52 |
{"role": "user", "content": user_prompt}
|
| 53 |
+
]
|
|
|
|
| 54 |
}
|
| 55 |
|
| 56 |
+
response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
|
| 57 |
return response.json()
|
| 58 |
|
| 59 |
except Exception as e:
|
| 60 |
return {"error": str(e)}
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
if __name__ == "__main__":
|
| 63 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|