Spaces:
Runtime error
Runtime error
Kaan
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,25 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
-
|
| 4 |
-
from huggingface_hub import hf_hub_download
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
tokenizer = AutoTokenizer.from_pretrained("./mistral-7b-v0.1.Q4_K_M.gguf")
|
| 13 |
-
model = AutoModelForCausalLM.from_pretrained("./mistral-7b-v0.1.Q4_K_M.gguf")
|
| 14 |
-
inputs = tokenizer(prompt, return_tensors="pt")
|
| 15 |
-
output = model.generate(input_ids=inputs["input_ids"], max_length=50, num_return_sequences=3, temperature=0.7)
|
| 16 |
-
generated_texts = tokenizer.batch_decode(output, skip_special_tokens=True)
|
| 17 |
-
for i, text in enumerate(generated_texts):
|
| 18 |
-
print(f"Generated Text {i+1}: {text}")
|
| 19 |
-
return generated_texts
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|
| 16 |
+
@app.get("/")
|
| 17 |
+
async def generate_text():
|
| 18 |
+
try:
|
| 19 |
+
prompt = "Once upon a time, there was a"
|
| 20 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 21 |
+
output = model.generate(input_ids=inputs["input_ids"], max_length=50, num_return_sequences=3, temperature=0.7)
|
| 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))
|