Kaan commited on
Commit
291ac95
·
verified ·
1 Parent(s): 0314811

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -3
app.py CHANGED
@@ -1,15 +1,22 @@
1
  from fastapi import FastAPI, HTTPException
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
 
3
  import os
4
 
5
  app = FastAPI()
6
 
7
  cache_dir = os.path.expanduser("~/.cache/huggingface/")
8
  model_folder = os.path.join(cache_dir, "TheBloke/Mistral-7B-v0.1-GGUF")
 
 
9
  if not os.path.exists(model_folder):
10
- raise ValueError("Model not found in cache directory. Please download the model.")
 
 
 
 
11
 
12
- # Load the tokenizer and model asynchronously
13
  tokenizer = AutoTokenizer.from_pretrained(model_folder)
14
  model = AutoModelForCausalLM.from_pretrained(model_folder)
15
 
@@ -22,4 +29,4 @@ async def generate_text():
22
  generated_texts = tokenizer.batch_decode(output, skip_special_tokens=True)
23
  return generated_texts
24
  except Exception as e:
25
- raise HTTPException(status_code=500, detail=str(e))
 
1
  from fastapi import FastAPI, HTTPException
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ from huggingface_hub import hf_hub_download
4
  import os
5
 
6
  app = FastAPI()
7
 
8
  cache_dir = os.path.expanduser("~/.cache/huggingface/")
9
  model_folder = os.path.join(cache_dir, "TheBloke/Mistral-7B-v0.1-GGUF")
10
+
11
+ # Check if the model is present in the cache directory
12
  if not os.path.exists(model_folder):
13
+ # Download the model if not present
14
+ try:
15
+ hf_hub_download(repo_id="TheBloke/Mistral-7B-v0.1-GGUF", filename="mistral-7b-v0.1.Q4_K_M.gguf")
16
+ except Exception as e:
17
+ raise HTTPException(status_code=500, detail=f"Failed to download model: {str(e)}")
18
 
19
+ # Load the tokenizer and model
20
  tokenizer = AutoTokenizer.from_pretrained(model_folder)
21
  model = AutoModelForCausalLM.from_pretrained(model_folder)
22
 
 
29
  generated_texts = tokenizer.batch_decode(output, skip_special_tokens=True)
30
  return generated_texts
31
  except Exception as e:
32
+ raise HTTPException(status_code=500, detail=str(e))