File size: 2,146 Bytes
13474d0
a754a3f
 
13474d0
a754a3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3cab35
 
 
 
a754a3f
3374a06
 
a754a3f
 
 
 
 
 
 
f3cab35
 
a754a3f
f3cab35
 
 
a754a3f
f3cab35
 
 
 
 
 
a754a3f
f3cab35
 
 
 
 
 
 
 
 
 
 
a754a3f
 
f3cab35
 
 
 
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
57
58
59
60
61
62
63
64
65
66
import os
import subprocess
import sys

# --- ZONA MAGICĂ DE INSTALARE ---
# Verificăm dacă llama_cpp e instalat. Dacă nu, îl instalăm acum, la pornire.
try:
    import llama_cpp
    print("✅ Llama-cpp-python este deja instalat.")
except ImportError:
    print("⏳ Se instalează llama-cpp-python pentru CPU (poate dura 30-60 secunde)...")
    # Această comandă folosește index-ul oficial abetlen, care găsește singur versiunea corectă
    subprocess.check_call([
        sys.executable, "-m", "pip", "install", 
        "llama-cpp-python", 
        "--no-cache-dir", 
        "--extra-index-url", "https://abetlen.github.io/llama-cpp-python/whl/cpu"
    ])
    print("✅ Instalare completă!")

# --- ACUM IMPORTĂM BIBLIOTECILE ---
import gradio as gr
from llama_cpp import Llama
from huggingface_hub import hf_hub_download

# --- CONFIGURARE ---
REPO_ID = "i04n4/llama3.2-3b-math-gguf" # <--- Verifică să fie numele tău corect
FILENAME = "model.gguf"   # <--- Verifică numele fișierului

print(f"⏳ Descarc modelul {FILENAME}...")
try:
    model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
except Exception as e:
    print(f"❌ Eroare la descărcare: {e}")
    raise e

print("🚀 Încarc motorul Llama...")
# n_threads=2 este optim pentru Free Tier
llm = Llama(model_path=model_path, n_ctx=2048, n_threads=2)

def generate_response(message, history):
    system_prompt = "You are a helpful mathematical assistant. Answer directly and precisely."
    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"
    
    response = ""
    output = llm(
        full_prompt, 
        max_tokens=512, 
        stop=["<|eot_id|>"], 
        echo=False, 
        stream=True
    )
    
    for chunk in output:
        delta = chunk['choices'][0]['text']
        response += delta
        yield response

demo = gr.ChatInterface(
    generate_response, 
    title="Math tutor",
    description="Ask me a question vro<3",
)

if __name__ == "__main__":
    demo.launch()