Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,14 +5,14 @@ import requests
|
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
MODEL_URL = "https://huggingface.co/TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF/resolve/main/TinyLlama-1.1B-Chat-v1.0.Q4_K_M.gguf
|
| 10 |
MODEL_PATH = "TinyLlama-1.1B-Chat-v1.0.Q4_K_M.gguf"
|
| 11 |
|
| 12 |
-
# Download
|
| 13 |
if not os.path.exists(MODEL_PATH) or os.path.getsize(MODEL_PATH) < 1000000:
|
| 14 |
-
print("Downloading TinyLlama model... (this happens once)")
|
| 15 |
-
headers = {"User-Agent": "Mozilla/5.0"}
|
| 16 |
r = requests.get(MODEL_URL, headers=headers, stream=True)
|
| 17 |
if r.status_code != 200:
|
| 18 |
raise ValueError(f"Failed to download model! HTTP {r.status_code}")
|
|
@@ -22,13 +22,10 @@ if not os.path.exists(MODEL_PATH) or os.path.getsize(MODEL_PATH) < 1000000:
|
|
| 22 |
f.write(chunk)
|
| 23 |
print("✅ Model downloaded!")
|
| 24 |
|
| 25 |
-
|
| 26 |
-
print("Loading TinyLlama model...")
|
| 27 |
llm = Llama(model_path=MODEL_PATH)
|
| 28 |
print("✅ TinyLlama is ready!")
|
| 29 |
|
| 30 |
-
|
| 31 |
-
# === API Endpoint ===
|
| 32 |
@app.route("/api/ask", methods=["POST"])
|
| 33 |
def ask():
|
| 34 |
data = request.get_json()
|
|
@@ -44,10 +41,8 @@ def ask():
|
|
| 44 |
top_p=0.9,
|
| 45 |
stop=["User:", "TinyLlama:"],
|
| 46 |
)
|
| 47 |
-
|
| 48 |
reply = output["choices"][0]["text"].strip()
|
| 49 |
return jsonify({"reply": reply})
|
| 50 |
|
| 51 |
-
|
| 52 |
if __name__ == "__main__":
|
| 53 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
+
# Correct model URL and filename
|
| 9 |
+
MODEL_URL = "https://huggingface.co/TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF/resolve/main/TinyLlama-1.1B-Chat-v1.0.Q4_K_M.gguf"
|
| 10 |
MODEL_PATH = "TinyLlama-1.1B-Chat-v1.0.Q4_K_M.gguf"
|
| 11 |
|
| 12 |
+
# Download model if missing or incomplete
|
| 13 |
if not os.path.exists(MODEL_PATH) or os.path.getsize(MODEL_PATH) < 1000000:
|
| 14 |
+
print("📦 Downloading TinyLlama model... (this happens once)")
|
| 15 |
+
headers = {"User-Agent": "Mozilla/5.0"}
|
| 16 |
r = requests.get(MODEL_URL, headers=headers, stream=True)
|
| 17 |
if r.status_code != 200:
|
| 18 |
raise ValueError(f"Failed to download model! HTTP {r.status_code}")
|
|
|
|
| 22 |
f.write(chunk)
|
| 23 |
print("✅ Model downloaded!")
|
| 24 |
|
| 25 |
+
print("🦙 Loading TinyLlama model...")
|
|
|
|
| 26 |
llm = Llama(model_path=MODEL_PATH)
|
| 27 |
print("✅ TinyLlama is ready!")
|
| 28 |
|
|
|
|
|
|
|
| 29 |
@app.route("/api/ask", methods=["POST"])
|
| 30 |
def ask():
|
| 31 |
data = request.get_json()
|
|
|
|
| 41 |
top_p=0.9,
|
| 42 |
stop=["User:", "TinyLlama:"],
|
| 43 |
)
|
|
|
|
| 44 |
reply = output["choices"][0]["text"].strip()
|
| 45 |
return jsonify({"reply": reply})
|
| 46 |
|
|
|
|
| 47 |
if __name__ == "__main__":
|
| 48 |
app.run(host="0.0.0.0", port=7860)
|