VitaliiPuzhenko commited on
Commit
de9ab11
·
verified ·
1 Parent(s): 4e5b21c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -4
app.py CHANGED
@@ -1,23 +1,27 @@
1
- from fastapi import FastAPI, Request, HTTPException
2
  from transformers import pipeline
3
  import os
 
4
 
5
  app = FastAPI()
6
 
7
- cache_dir = os.path.expanduser("~/.cache/huggingface/transformers")
 
8
  os.environ["TRANSFORMERS_CACHE"] = cache_dir
 
9
 
10
  try:
11
  pipe = pipeline(
12
  "text-generation",
13
  model="google/gemma-3-1b-it",
14
- token=os.environ["HF_TOKEN"],
15
  device="cpu",
16
  max_new_tokens=256,
17
  temperature=0.7
18
  )
19
  except Exception as e:
20
- raise HTTPException(status_code=500, detail=str(e))
 
21
 
22
  @app.post("/generate")
23
  async def generate(request: Request):
@@ -28,3 +32,4 @@ async def generate(request: Request):
28
  return {"response": result[0]["generated_text"]}
29
  except Exception as e:
30
  raise HTTPException(status_code=500, detail=str(e))
 
 
1
+ from fastapi import FastAPI, Request
2
  from transformers import pipeline
3
  import os
4
+ import tempfile
5
 
6
  app = FastAPI()
7
 
8
+ # Використання тимчасової директорії для кешу
9
+ cache_dir = tempfile.mkdtemp()
10
  os.environ["TRANSFORMERS_CACHE"] = cache_dir
11
+ print(f"Використовується директорія кешу: {cache_dir}")
12
 
13
  try:
14
  pipe = pipeline(
15
  "text-generation",
16
  model="google/gemma-3-1b-it",
17
+ token=os.environ.get("HF_TOKEN"), # Краще використовувати get для безпеки
18
  device="cpu",
19
  max_new_tokens=256,
20
  temperature=0.7
21
  )
22
  except Exception as e:
23
+ print(f"Помилка при ініціалізації pipeline: {e}")
24
+ # Тут можна обробити помилку або підняти HTTP виняток
25
 
26
  @app.post("/generate")
27
  async def generate(request: Request):
 
32
  return {"response": result[0]["generated_text"]}
33
  except Exception as e:
34
  raise HTTPException(status_code=500, detail=str(e))
35
+