Download app.py from GoshawkVortexAI/Goshawk_GeminiPro: direct link, hf CLI and curl.
- Browser
- Download file 6.92 kB
-
https://huggingface.co/spaces/GoshawkVortexAI/Goshawk_GeminiPro/resolve/main/app.py
- Command line
-
hf download hf://spaces/GoshawkVortexAI/Goshawk_GeminiPro/app.py
-
curl -L -o app.py https://huggingface.co/spaces/GoshawkVortexAI/Goshawk_GeminiPro/resolve/main/app.py
6.92 kB
| import os | |
| import numpy as np | |
| import xgboost as xgb | |
| import gradio as gr | |
| from google import genai | |
| from google.genai import types | |
| # ========================================== | |
| # 🔌 1. GEMINI CLIENT BAŞLATMA | |
| # ========================================== | |
| api_key = os.environ.get("GEMINI_API_KEY") | |
| client = None | |
| if api_key: | |
| client = genai.Client(api_key=api_key) | |
| # ========================================== | |
| # 📊 2. SENTETİK XGBOOST MODELİ (MOCK TRAIN) | |
| # ========================================== | |
| # Arayüz açıldığında hızlıca kalibre edilen örnek XGBoost sınıflandırıcısı. | |
| # Özellikler: [Ev_Sırası, Dep_Sırası, Ev_Form_Puanı, Dep_Form_Puanı, Ev_Sakat_Sayısı, Dep_Sakat_Sayısı] | |
| def train_baseline_xgboost(): | |
| X_synthetic = np.array([ | |
| [1, 18, 13, 3, 0, 3], # Güçlü ev sahibi favori | |
| [15, 2, 4, 12, 2, 0], # Güçlü deplasman favori | |
| [8, 9, 7, 8, 1, 1], # Denk takımlar | |
| [4, 5, 10, 9, 0, 1], # Kafa kafaya zirve mücadelesi | |
| [20, 19, 2, 1, 3, 2], # Alt sıraların mücadelesi | |
| [3, 16, 11, 4, 1, 2], # Ev favori | |
| [17, 4, 3, 11, 2, 0] # Deplasman favori | |
| ]) | |
| # Hedef Değişkenler: 0 -> Ev Sahibi Kazanır, 1 -> Beraberlik, 2 -> Deplasman Kazanır | |
| y_synthetic = np.array([0, 2, 1, 0, 1, 0, 2]) | |
| model = xgb.XGBClassifier( | |
| n_estimators=30, | |
| max_depth=3, | |
| learning_rate=0.1, | |
| objective='multi:softprob', | |
| num_class=3, | |
| eval_metric='mlogloss', | |
| random_state=42 | |
| ) | |
| model.fit(X_synthetic, y_synthetic) | |
| return model | |
| # Uygulama başlarken modeli hafızaya yükle | |
| xgb_model = train_baseline_xgboost() | |
| # ========================================== | |
| # 🧠 3. TAHMİN VE ANALİZ MOTORU | |
| # ========================================== | |
| def predict_and_analyze(home_team, away_team, home_rank, away_rank, home_form, away_form, home_injuries, away_injuries): | |
| if not home_team or not away_team: | |
| return "Lütfen ev sahibi ve deplasman takımlarının isimlerini giriniz.", {} | |
| # XGBoost Tahmin Girdisini Hazırlama | |
| features = np.array([[home_rank, away_rank, home_form, away_form, home_injuries, away_injuries]]) | |
| probabilities = xgb_model.predict_proba(features)[0] | |
| p_home = float(probabilities[0]) | |
| p_draw = float(probabilities[1]) | |
| p_away = float(probabilities[2]) | |
| # Gradio grafiği için dictionary formatı | |
| prob_dict = { | |
| f"{home_team} Galibiyeti (1)": p_home, | |
| "Beraberlik (X)": p_draw, | |
| f"{away_team} Galibiyeti (2)": p_away | |
| } | |
| # Gemini Analiz Promptu | |
| prompt = f""" | |
| Sen uzman bir spor analisti ve profesyonel bir veri modelleyicisisin. | |
| Karşılaşma: {home_team} (Ev) vs {away_team} (Deplasman) | |
| İstastistiksel Göstergeler: | |
| - {home_team}: Lig Sıralaması: {home_rank}, Son 5 Maç Form Puanı (Max 15): {home_form}, Eksik/Sakat Oyuncu: {home_injuries} | |
| - {away_team}: Lig Sıralaması: {away_rank}, Son 5 Maç Form Puanı (Max 15): {away_form}, Eksik/Sakat Oyuncu: {away_injuries} | |
| XGBoost Makine Öğrenmesi Modeli Sonuçları: | |
| - {home_team} Kazanma Olasılığı: %{p_home*100:.1f} | |
| - Beraberlik Olasılığı: %{p_draw*100:.1f} | |
| - {away_team} Kazanma Olasılığı: %{p_away*100:.1f} | |
| Yukarıdaki matematiksel olasılıkları ve takım durumlarını analiz ederek Türkçe bir rapor hazırla. Raporda şu başlıklar mutlaka bulunsun: | |
| 1. 🎯 Model Yorumu (XGBoost olasılıklarının mantığı) | |
| 2. ⚽ Olası Skor ve Gol Tahmini (Alt/Üst değerlendirmesi) | |
| 3. 💡 Değerli Bahis Önerisi (+EV Değerlendirmesi) | |
| Yorumların rasyonel, nesnel ve profesyonel olsun. | |
| """ | |
| # Gemini API Çağrısı | |
| if client: | |
| try: | |
| response = client.models.generate_content( | |
| model='gemini-2.5-flash', | |
| contents=prompt, | |
| config=types.GenerateContentConfig( | |
| temperature=0.3 | |
| ) | |
| ) | |
| analysis_result = response.text | |
| except Exception as e: | |
| analysis_result = f"Gemini API Analiz Hatası: {str(e)}\n\n(Lütfen GEMINI_API_KEY değişkeninizi Hugging Face Secrets alanına doğru eklediğinizden emin olun.)" | |
| else: | |
| analysis_result = "Gemini API Anahtarı bulunamadı! Lütfen Hugging Face Space alanına 'GEMINI_API_KEY' adıyla bir Secret ekleyin. (Yine de yukarıdaki XGBoost grafik tahminini görebilirsiniz.)" | |
| return analysis_result, prob_dict | |
| # ========================================== | |
| # 🎨 4. GRADIO ARAYÜZÜ (DARK THEME) | |
| # ========================================== | |
| with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="slate")) as demo: | |
| gr.Markdown( | |
| """ | |
| # ⚽ XGBoost & Gemini Hibrit Maç Tahmin Radarı | |
| Bu uygulama, girdiğiniz takım verilerini **XGBoost** sınıflandırıcısı ile tarayıp olasılıkları hesaplar ve **Gemini** yapay zekası ile matematiksel olarak yorumlar. | |
| """ | |
| ) | |
| with gr.Row(): | |
| with gr.Column(): | |
| gr.Markdown("### 🏟️ Takım İsimleri") | |
| home_input = gr.Textbox(label="Ev Sahibi Takım", value="Arsenal") | |
| away_input = gr.Textbox(label="Deplasman Takım", value="Chelsea") | |
| gr.Markdown("### 📊 Lig ve Form Durumu") | |
| with gr.Row(): | |
| home_rank_input = gr.Number(label="Ev Sahibi Lig Sırası", value=3, precision=0) | |
| away_rank_input = gr.Number(label="Deplasman Lig Sırası", value=12, precision=0) | |
| with gr.Row(): | |
| home_form_input = gr.Slider(label="Ev Form Puanı (Son 5 Maç)", minimum=0, maximum=15, value=11, step=1) | |
| away_form_input = gr.Slider(label="Deplasman Form Puanı (Son 5 Maç)", minimum=0, maximum=15, value=4, step=1) | |
| with gr.Row(): | |
| home_inj_input = gr.Number(label="Ev Sakat/Cezalı Sayısı", value=1, precision=0) | |
| away_inj_input = gr.Number(label="Deplasman Sakat/Cezalı Sayısı", value=3, precision=0) | |
| submit_btn = gr.Button("Analiz Et ve Tahmin Üret", variant="primary") | |
| with gr.Column(): | |
| gr.Markdown("### 📈 Matematiksel Kazanma Olasılıkları (XGBoost)") | |
| prob_output = gr.Label(label="Olasılık Dağılımı") | |
| gr.Markdown("### 🧠 Gemini Yapay Zeka Analiz Raporu") | |
| analysis_output = gr.Markdown(value="Verileri girdikten sonra butona basarak analizi başlatabilirsiniz.") | |
| submit_btn.click( | |
| fn=predict_and_analyze, | |
| inputs=[ | |
| home_input, away_input, | |
| home_rank_input, away_rank_input, | |
| home_form_input, away_form_input, | |
| home_inj_input, away_inj_input | |
| ], | |
| outputs=[analysis_output, prob_output] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |