Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, Response, stream_with_context
|
| 2 |
+
from llama_cpp import Llama
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
|
| 7 |
+
# Model setup - Using the Gemma-3-1B Heretic GGUF
|
| 8 |
+
# Ensure this file is uploaded to your Space as well
|
| 9 |
+
llm = Llama(
|
| 10 |
+
model_path="gemma-3-1b-it-heretic-extreme-uncensored-abliterated.Q4_K_S.gguf",
|
| 11 |
+
n_ctx=1024,
|
| 12 |
+
n_threads=2, # Optimized for HF Free CPU
|
| 13 |
+
verbose=False
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
@app.route('/generate', methods=['POST'])
|
| 17 |
+
def generate():
|
| 18 |
+
data = request.json
|
| 19 |
+
prompt = data.get("prompt", "")
|
| 20 |
+
|
| 21 |
+
# System instruction for Jarvis personality
|
| 22 |
+
full_prompt = f"<|system|>You are Jarvis, a witty and concise AI assistant. Respond briefly.<|user|>{prompt}<|assistant|>"
|
| 23 |
+
|
| 24 |
+
@stream_with_context
|
| 25 |
+
def generate_tokens():
|
| 26 |
+
# stream=True is the key for word-by-word
|
| 27 |
+
output = llm(full_prompt, stream=True, max_tokens=150)
|
| 28 |
+
for chunk in output:
|
| 29 |
+
token = chunk['choices'][0]['text']
|
| 30 |
+
if token:
|
| 31 |
+
# Yielding the token immediately sends it to your laptop
|
| 32 |
+
yield token
|
| 33 |
+
|
| 34 |
+
return Response(generate_tokens(), mimetype='text/plain')
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
app.run(host="0.0.0.0", port=7860)
|