Yermek68 commited on
Commit
697670a
·
verified ·
1 Parent(s): 5f14208

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -102
app.py CHANGED
@@ -1,104 +1,22 @@
1
- import os
2
- import sys
3
- import time
4
- import json
5
- import logging
6
- import traceback
7
- from fastapi import FastAPI, HTTPException
8
- from fastapi.middleware.cors import CORSMiddleware
9
- from pydantic import BaseModel, Field
10
- from contextlib import asynccontextmanager
11
- from typing import Dict, Any
12
-
13
- # ======= ЛОГИРОВАНИЕ =======
14
- logging.basicConfig(
15
- level=logging.INFO,
16
- format="%(asctime)s [%(levelname)s] %(message)s",
17
- handlers=[logging.StreamHandler(sys.stderr)],
 
18
  )
19
- logger = logging.getLogger("eroha-api")
20
-
21
- # ======= ИМПОРТЫ =======
22
- try:
23
- from transformers import pipeline
24
- from langdetect import detect
25
- except Exception as e:
26
- logger.error("[ImportError] transformers/langdetect not available: %s", e, exc_info=True)
27
- pipeline = None
28
- def detect(text): return "en"
29
-
30
- # ======= НАСТРОЙКИ =======
31
- HF_HOME = "/tmp/huggingface"
32
- os.environ["HF_HOME"] = HF_HOME
33
- os.makedirs(HF_HOME, exist_ok=True)
34
-
35
- # ======= МОДЕЛИ =======
36
- _model_cache: Dict[str, Any] = {}
37
-
38
- def get_model(lang: str):
39
- if pipeline is None:
40
- raise RuntimeError("Transformers pipeline is not available")
41
- if lang in _model_cache:
42
- return _model_cache[lang]
43
- model_map = {
44
- "ru": "IlyaGusev/mbart_ru_sum_gazeta",
45
- "en": "facebook/bart-large-cnn",
46
- }
47
- model_name = model_map.get(lang, "facebook/bart-large-cnn")
48
- model = pipeline("summarization", model=model_name, device=-1)
49
- _model_cache[lang] = model
50
- return model
51
-
52
- # ======= FastAPI =======
53
- @asynccontextmanager
54
- async def lifespan(app: FastAPI):
55
- start = time.time()
56
- logger.info("[Startup] warming up models...")
57
- for lang in ("en", "ru"):
58
- try: get_model(lang)
59
- except Exception as e: logger.error("Warmup failed: %s", e)
60
- yield
61
- logger.info("[Shutdown] done")
62
-
63
- app = FastAPI(title="Eroha Agent API", version="v3.5", lifespan=lifespan)
64
- app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
65
-
66
- # ======= МОДЕЛИ ЗАПРОСОВ =======
67
- class SummarizeRequest(BaseModel):
68
- text: str = Field(..., min_length=3, max_length=1_000_000)
69
-
70
- class MemoryRequest(BaseModel):
71
- key: str
72
- content: str
73
-
74
- # ======= ЭНДПОИНТЫ =======
75
- @app.get("/")
76
- async def root():
77
- return {"status": "ok", "version": "v3.5"}
78
-
79
- @app.get("/ping")
80
- async def ping():
81
- return {"status": "healthy", "cache": list(_model_cache.keys())}
82
-
83
- @app.post("/summarize")
84
- async def summarize(req: SummarizeRequest):
85
- lang = "ru" if "а" in req.text.lower() else "en"
86
- model = get_model(lang)
87
- result = model(req.text[:2000], max_length=180, min_length=50, do_sample=False)
88
- return {"summary": result[0]["summary_text"].strip(), "lang": lang}
89
-
90
- # ======= MEMORY API =======
91
- @app.post("/memorize")
92
- async def memorize(req: MemoryRequest):
93
- with open("memory.json", "a") as f:
94
- f.write(json.dumps(req.dict(), ensure_ascii=False) + "\\n")
95
- return {"status": "saved"}
96
 
97
- @app.post("/retrieve")
98
- async def retrieve(req: MemoryRequest):
99
- if not os.path.exists("memory.json"):
100
- return {"found": []}
101
- with open("memory.json", "r") as f:
102
- lines = [json.loads(l) for l in f]
103
- found = [l for l in lines if req.key.lower() in l["key"].lower()]
104
- return {"found": found}
 
1
+ import gradio as gr
2
+ import huggingface_hub
3
+ from transformers import pipeline
4
+
5
+ # Загружаем модель для примера (можно заменить на свою)
6
+ generator = pipeline("text-generation", model="distilgpt2")
7
+
8
+ def chat_fn(prompt):
9
+ response = generator(prompt, max_length=50, num_return_sequences=1)[0]["generated_text"]
10
+ return f"🤖 Ответ модели:\n{response}\n\n📦 huggingface-hub версия: {huggingface_hub.__version__}"
11
+
12
+ # Интерфейс Gradio
13
+ demo = gr.Interface(
14
+ fn=chat_fn,
15
+ inputs="text",
16
+ outputs="text",
17
+ title="Eroha AgentAPI — стабильный Space",
18
+ description="Исправленный runtime с фиксированной версией huggingface-hub==0.23.4"
19
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
+ if __name__ == "__main__":
22
+ demo.launch(server_name="0.0.0.0", server_port=7860)