Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,9 +1,90 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
"role": "system",
|
| 3 |
"content": (
|
| 4 |
"Sen Hilman'sın. Halil'in otonom yapay zeka asistanısın. "
|
| 5 |
"İşletim sistemi WINDOWS'tur. Komut üretirken ASLA macOS ('open -a ...') komutları kullanma. "
|
| 6 |
"Tüm komutları doğrudan Windows CMD/PowerShell formatında yaz (Örn: ```cmd\nstart chrome\n```). "
|
| 7 |
-
"Yanıtlarını AŞIRI KISA tut (1-2 cümle).
|
|
|
|
| 8 |
)
|
| 9 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
from fastapi import FastAPI, Header, HTTPException
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
|
| 6 |
+
app = FastAPI(title="Hilman Universal AI Core")
|
| 7 |
+
|
| 8 |
+
MY_SECRET_KEY = os.getenv("MY_SECRET_KEY", "hilman-secret-2026")
|
| 9 |
+
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
|
| 10 |
+
ANTIGRAVITY_API_KEY = os.getenv("ANTIGRAVITY_API_KEY")
|
| 11 |
+
|
| 12 |
+
class ChatRequest(BaseModel):
|
| 13 |
+
provider: str = "antigravity"
|
| 14 |
+
model: str = "gemini-2.0-flash"
|
| 15 |
+
messages: list
|
| 16 |
+
|
| 17 |
+
@app.get("/")
|
| 18 |
+
def status():
|
| 19 |
+
return {"status": "Hilman AI Core Online", "system": "7/24 Active"}
|
| 20 |
+
|
| 21 |
+
@app.post("/v1/chat/completions")
|
| 22 |
+
def chat_proxy(req: ChatRequest, authorization: str = Header(None)):
|
| 23 |
+
if authorization != f"Bearer {MY_SECRET_KEY}":
|
| 24 |
+
raise HTTPException(status_code=401, detail="Yetkisiz erişim!")
|
| 25 |
+
|
| 26 |
+
compressed_messages = req.messages[-4:] if len(req.messages) > 4 else req.messages
|
| 27 |
+
|
| 28 |
+
# WINDOWS Odaklı Sistem Talimatı
|
| 29 |
+
system_instruction = {
|
| 30 |
"role": "system",
|
| 31 |
"content": (
|
| 32 |
"Sen Hilman'sın. Halil'in otonom yapay zeka asistanısın. "
|
| 33 |
"İşletim sistemi WINDOWS'tur. Komut üretirken ASLA macOS ('open -a ...') komutları kullanma. "
|
| 34 |
"Tüm komutları doğrudan Windows CMD/PowerShell formatında yaz (Örn: ```cmd\nstart chrome\n```). "
|
| 35 |
+
"Yanıtlarını AŞIRI KISA tut (1-2 cümle). "
|
| 36 |
+
"Komut çalıştıracaksan sadece gerekli ```cmd ... ``` bloğunu ve kısa bir onay yaz."
|
| 37 |
)
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
full_messages = [system_instruction] + compressed_messages
|
| 41 |
+
|
| 42 |
+
# 1. ÖNCELİK: ANTIGRAVITY API
|
| 43 |
+
if ANTIGRAVITY_API_KEY:
|
| 44 |
+
ag_headers = {
|
| 45 |
+
"Authorization": f"Bearer {ANTIGRAVITY_API_KEY.strip()}",
|
| 46 |
+
"Content-Type": "application/json"
|
| 47 |
+
}
|
| 48 |
+
ag_payload = {
|
| 49 |
+
"model": "gemini-2.0-flash",
|
| 50 |
+
"messages": full_messages,
|
| 51 |
+
"temperature": 0.1
|
| 52 |
+
}
|
| 53 |
+
try:
|
| 54 |
+
print("[SİSTEM]: Antigravity API deneniyor...")
|
| 55 |
+
ag_res = requests.post("https://api.antigravity.dev/v1/chat/completions", json=ag_payload, headers=ag_headers, timeout=25)
|
| 56 |
+
if ag_res.status_code == 200:
|
| 57 |
+
print("[BAŞARILI]: Antigravity yanıt verdi!")
|
| 58 |
+
return ag_res.json()
|
| 59 |
+
else:
|
| 60 |
+
print(f"[ANTIGRAVITY HATASI]: {ag_res.status_code} - {ag_res.text}")
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(f"[ANTIGRAVITY BAĞLANTI HATASI]: {e}")
|
| 63 |
+
|
| 64 |
+
# 2. ÖNCELİK (YEDEK): OPENROUTER API
|
| 65 |
+
if OPENROUTER_API_KEY:
|
| 66 |
+
or_headers = {
|
| 67 |
+
"Authorization": f"Bearer {OPENROUTER_API_KEY.strip()}",
|
| 68 |
+
"Content-Type": "application/json",
|
| 69 |
+
"HTTP-Referer": "https://huggingface.co",
|
| 70 |
+
"X-Title": "Hilman AI"
|
| 71 |
+
}
|
| 72 |
+
or_models = [
|
| 73 |
+
"google/gemini-2.0-flash-lite-001",
|
| 74 |
+
"deepseek/deepseek-r1-distill-llama-70b"
|
| 75 |
+
]
|
| 76 |
+
for m in or_models:
|
| 77 |
+
try:
|
| 78 |
+
print(f"[SİSTEM]: OpenRouter ({m}) deneniyor...")
|
| 79 |
+
or_res = requests.post(
|
| 80 |
+
"https://openrouter.ai/api/v1/chat/completions",
|
| 81 |
+
json={"model": m, "messages": full_messages, "temperature": 0.1},
|
| 82 |
+
headers=or_headers,
|
| 83 |
+
timeout=15
|
| 84 |
+
)
|
| 85 |
+
if or_res.status_code == 200:
|
| 86 |
+
return or_res.json()
|
| 87 |
+
except Exception:
|
| 88 |
+
continue
|
| 89 |
+
|
| 90 |
+
raise HTTPException(status_code=503, detail="Tüm servisler zaman aşımına uğradı veya meşgul.")
|