Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,56 +1,56 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
1 |
+
import os
|
2 |
+
from fastapi import FastAPI, Request
|
3 |
+
from fastapi.responses import JSONResponse
|
4 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
5 |
+
|
6 |
+
# Set up safe cache directory for Hugging Face
|
7 |
+
cache_dir = os.getenv("TRANSFORMERS_CACHE", "/cache") # Use environment variable or default to /cache
|
8 |
+
os.makedirs(cache_dir, exist_ok=True)
|
9 |
+
|
10 |
+
# Optional: Use token only if you're accessing a private model
|
11 |
+
hf_token = os.getenv("HF_TOKEN")
|
12 |
+
|
13 |
+
# Load tokenizer and model
|
14 |
+
model_id = "mistralai/Mistral-7B-Instruct-v0.2"
|
15 |
+
|
16 |
+
try:
|
17 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, token=hf_token, cache_dir=cache_dir)
|
18 |
+
model = AutoModelForCausalLM.from_pretrained(
|
19 |
+
model_id,
|
20 |
+
token=hf_token,
|
21 |
+
cache_dir=cache_dir,
|
22 |
+
device_map="auto", # or "cpu" if no GPU
|
23 |
+
torch_dtype="auto" # will default to float32 on CPU
|
24 |
+
)
|
25 |
+
except Exception as e:
|
26 |
+
raise RuntimeError(f"Failed to load model or tokenizer: {str(e)}")
|
27 |
+
|
28 |
+
# Load pipeline
|
29 |
+
pipe = pipeline(
|
30 |
+
"text-generation",
|
31 |
+
model=model,
|
32 |
+
tokenizer=tokenizer,
|
33 |
+
max_new_tokens=256,
|
34 |
+
temperature=0.7,
|
35 |
+
top_p=0.9,
|
36 |
+
repetition_penalty=1.1,
|
37 |
+
)
|
38 |
+
|
39 |
+
# Init FastAPI app
|
40 |
+
app = FastAPI()
|
41 |
+
|
42 |
+
@app.post("/api")
|
43 |
+
async def ask_ai(request: Request):
|
44 |
+
try:
|
45 |
+
data = await request.json()
|
46 |
+
question = data.get("question", "").strip()
|
47 |
+
|
48 |
+
if not question:
|
49 |
+
return JSONResponse(content={"answer": "❗ Please enter a valid question."})
|
50 |
+
|
51 |
+
prompt = f"[INST] {question} [/INST]"
|
52 |
+
output = pipe(prompt)[0]["generated_text"]
|
53 |
+
return JSONResponse(content={"answer": output.strip()})
|
54 |
+
|
55 |
+
except Exception as e:
|
56 |
+
return JSONResponse(content={"answer": f"⚠️ Error: {str(e)}"})
|