Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from llama_cpp import Llama
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
REPO_ID = "i04n4/math-solver-llama3-Q8_0-GGUF"
|
| 5 |
+
FILENAME = "model.gguf"
|
| 6 |
+
|
| 7 |
+
model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
|
| 8 |
+
|
| 9 |
+
print("🚀 Încarc motorul Llama...")
|
| 10 |
+
llm = Llama(model_path=model_path, n_ctx=2048, n_threads=2)
|
| 11 |
+
|
| 12 |
+
def generate_response(message, history):
|
| 13 |
+
system_prompt = """You are a specialized mathematical assistant. Your goal is to be precise and efficient.
|
| 14 |
+
|
| 15 |
+
RULES:
|
| 16 |
+
- RESPONSE TYPE A (Basic/Fact-based): If the question is simple arithmetic (e.g., 2+2) or a basic fact, provide ONLY the final result. Be as concise as possible.
|
| 17 |
+
- RESPONSE TYPE B (Complex/Logic): If the question requires multiple steps, algebra, or reasoning, provide a clear 'Step-by-step reasoning' followed by 'Final Answer'.
|
| 18 |
+
- FORMATTING: Never use internal scratchpad tags like <<...>>. Use plain text only. Do not use your inner thougths, only the explanation. Do not use any symbols that are not needed
|
| 19 |
+
- LANGUAGE: Always answer in English."""
|
| 20 |
+
|
| 21 |
+
full_prompt = f"<|start_header_id|>system<|end_header_id|>\n\n{system_prompt}<|eot_id|><|start_header_id|>user<|end_header_id|>\n\n{message}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
|
| 22 |
+
|
| 23 |
+
response = ""
|
| 24 |
+
output = llm(
|
| 25 |
+
full_prompt,
|
| 26 |
+
max_tokens=512,
|
| 27 |
+
stop=["<|eot_id|>", "User:"],
|
| 28 |
+
echo=False,
|
| 29 |
+
stream=True
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
for chunk in output:
|
| 33 |
+
delta = chunk['choices'][0]['text']
|
| 34 |
+
response += delta
|
| 35 |
+
yield response
|
| 36 |
+
|
| 37 |
+
demo = gr.ChatInterface(
|
| 38 |
+
generate_response,
|
| 39 |
+
title="Super Awesome Math Tutor",
|
| 40 |
+
description="Ask me a math question!!!!!!",
|
| 41 |
+
examples=["Calculate 25 * 14", "Solve 2x + 5 = 15", "Derivative of x^2"]
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
demo.launch()
|