Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -343,65 +343,68 @@ IS_HF_SPACES = os.getenv("SPACE_ID") is not None
|
|
| 343 |
import os
|
| 344 |
import gradio as gr
|
| 345 |
import uvicorn
|
|
|
|
| 346 |
from fastapi import FastAPI
|
| 347 |
from gradio.routes import mount_gradio_app
|
| 348 |
|
| 349 |
-
#
|
|
|
|
|
|
|
| 350 |
try:
|
| 351 |
from alert_core import log_alert
|
| 352 |
except ModuleNotFoundError:
|
| 353 |
-
def log_alert(message):
|
| 354 |
-
print(f"[⚠️ ALERT] {message} (alert_core not found,
|
| 355 |
|
| 356 |
-
#
|
| 357 |
-
#
|
| 358 |
-
#
|
| 359 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
with demo:
|
| 362 |
gr.Markdown("# 🤖 Eroha AgentAPI v5.9.2 — Enterprise Edition")
|
| 363 |
-
|
| 364 |
with gr.Tab("💬 Chat"):
|
| 365 |
inp = gr.Textbox(label="Введите запрос")
|
| 366 |
model = gr.Dropdown(
|
| 367 |
["microsoft/phi-3-mini-4k-instruct",
|
| 368 |
"google/gemma-2-2b-it",
|
| 369 |
"meta-llama/Meta-Llama-3-8B-Instruct"],
|
| 370 |
-
value="microsoft/phi-3-mini-4k-instruct",
|
|
|
|
| 371 |
)
|
| 372 |
out = gr.Textbox(label="Ответ")
|
| 373 |
btn = gr.Button("🚀 Отправить")
|
| 374 |
btn.click(fn=lambda x, m: f"Обработка запроса для {m}: {x}",
|
| 375 |
inputs=[inp, model],
|
| 376 |
outputs=out)
|
| 377 |
-
|
| 378 |
with gr.Tab("📊 Dashboard"):
|
| 379 |
-
dash = gr.Markdown("📈
|
| 380 |
refresh = gr.Button("🔄 Обновить")
|
| 381 |
-
refresh.click(fn=lambda: "Метрики обновлены
|
| 382 |
|
| 383 |
-
#
|
| 384 |
-
#
|
| 385 |
-
#
|
| 386 |
IS_HF_SPACES = os.getenv("SPACE_ID") is not None
|
| 387 |
RUN_ENV = "Hugging Face Spaces" if IS_HF_SPACES else "Localhost"
|
| 388 |
|
| 389 |
-
|
| 390 |
-
logging.basicConfig(
|
| 391 |
-
level=logging.INFO,
|
| 392 |
-
format="%(asctime)s [%(levelname)s] %(message)s",
|
| 393 |
-
handlers=[logging.StreamHandler()]
|
| 394 |
-
)
|
| 395 |
-
|
| 396 |
-
logging.info(f"🚀 Starting Eroha Agent Environment: {RUN_ENV}")
|
| 397 |
log_alert(f"System boot: {RUN_ENV}")
|
| 398 |
|
| 399 |
-
# Создаём FastAPI приложение
|
| 400 |
-
app = FastAPI()
|
| 401 |
-
|
| 402 |
-
# =====================================================================
|
| 403 |
-
# ✅ Финальный блок запуска
|
| 404 |
-
# =====================================================================
|
| 405 |
if __name__ == "__main__":
|
| 406 |
if IS_HF_SPACES:
|
| 407 |
logging.info("✅ Running on Hugging Face Spaces (port 7860)")
|
|
@@ -409,7 +412,6 @@ if __name__ == "__main__":
|
|
| 409 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 410 |
else:
|
| 411 |
import threading
|
| 412 |
-
|
| 413 |
logging.info("✅ Running locally (FastAPI → 7860 | Gradio → 7861)")
|
| 414 |
|
| 415 |
def run_gradio():
|
|
@@ -417,4 +419,3 @@ if __name__ == "__main__":
|
|
| 417 |
|
| 418 |
threading.Thread(target=run_gradio, daemon=True).start()
|
| 419 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 420 |
-
|
|
|
|
| 343 |
import os
|
| 344 |
import gradio as gr
|
| 345 |
import uvicorn
|
| 346 |
+
import logging
|
| 347 |
from fastapi import FastAPI
|
| 348 |
from gradio.routes import mount_gradio_app
|
| 349 |
|
| 350 |
+
# ==========================================================
|
| 351 |
+
# 🔒 Safe import (если alert_core нет — используем fallback)
|
| 352 |
+
# ==========================================================
|
| 353 |
try:
|
| 354 |
from alert_core import log_alert
|
| 355 |
except ModuleNotFoundError:
|
| 356 |
+
def log_alert(message: str):
|
| 357 |
+
print(f"[⚠️ ALERT] {message} (alert_core not found, fallback active)")
|
| 358 |
|
| 359 |
+
# ==========================================================
|
| 360 |
+
# 🧠 Настройка логирования (в консоль и файл)
|
| 361 |
+
# ==========================================================
|
| 362 |
+
os.makedirs("logs", exist_ok=True)
|
| 363 |
+
logging.basicConfig(
|
| 364 |
+
level=logging.INFO,
|
| 365 |
+
format="%(asctime)s [%(levelname)s] %(message)s",
|
| 366 |
+
handlers=[
|
| 367 |
+
logging.StreamHandler(),
|
| 368 |
+
logging.FileHandler("logs/startup.log", mode="a", encoding="utf-8")
|
| 369 |
+
]
|
| 370 |
+
)
|
| 371 |
|
| 372 |
+
# ==========================================================
|
| 373 |
+
# 🌐 Создаём FastAPI и интерфейс Gradio
|
| 374 |
+
# ==========================================================
|
| 375 |
+
app = FastAPI()
|
| 376 |
+
|
| 377 |
+
demo = gr.Blocks(title="Eroha AgentAPI v5.9.2 — Enterprise Edition")
|
| 378 |
with demo:
|
| 379 |
gr.Markdown("# 🤖 Eroha AgentAPI v5.9.2 — Enterprise Edition")
|
|
|
|
| 380 |
with gr.Tab("💬 Chat"):
|
| 381 |
inp = gr.Textbox(label="Введите запрос")
|
| 382 |
model = gr.Dropdown(
|
| 383 |
["microsoft/phi-3-mini-4k-instruct",
|
| 384 |
"google/gemma-2-2b-it",
|
| 385 |
"meta-llama/Meta-Llama-3-8B-Instruct"],
|
| 386 |
+
value="microsoft/phi-3-mini-4k-instruct",
|
| 387 |
+
label="Модель"
|
| 388 |
)
|
| 389 |
out = gr.Textbox(label="Ответ")
|
| 390 |
btn = gr.Button("🚀 Отправить")
|
| 391 |
btn.click(fn=lambda x, m: f"Обработка запроса для {m}: {x}",
|
| 392 |
inputs=[inp, model],
|
| 393 |
outputs=out)
|
|
|
|
| 394 |
with gr.Tab("📊 Dashboard"):
|
| 395 |
+
dash = gr.Markdown("📈 Метрики ещё не загружены")
|
| 396 |
refresh = gr.Button("🔄 Обновить")
|
| 397 |
+
refresh.click(fn=lambda: "✅ Метрики обновлены", outputs=dash)
|
| 398 |
|
| 399 |
+
# ==========================================================
|
| 400 |
+
# 🧭 Проверка окружения и запуск
|
| 401 |
+
# ==========================================================
|
| 402 |
IS_HF_SPACES = os.getenv("SPACE_ID") is not None
|
| 403 |
RUN_ENV = "Hugging Face Spaces" if IS_HF_SPACES else "Localhost"
|
| 404 |
|
| 405 |
+
logging.info(f"🚀 Starting Eroha Agent in environment: {RUN_ENV}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
log_alert(f"System boot: {RUN_ENV}")
|
| 407 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 408 |
if __name__ == "__main__":
|
| 409 |
if IS_HF_SPACES:
|
| 410 |
logging.info("✅ Running on Hugging Face Spaces (port 7860)")
|
|
|
|
| 412 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 413 |
else:
|
| 414 |
import threading
|
|
|
|
| 415 |
logging.info("✅ Running locally (FastAPI → 7860 | Gradio → 7861)")
|
| 416 |
|
| 417 |
def run_gradio():
|
|
|
|
| 419 |
|
| 420 |
threading.Thread(target=run_gradio, daemon=True).start()
|
| 421 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|