File size: 2,638 Bytes
563c4a2
 
b41a63b
563c4a2
d4ad754
 
563c4a2
 
74f331c
563c4a2
 
74f331c
563c4a2
841cdf1
 
74f331c
841cdf1
d4ad754
841cdf1
74f331c
d4ad754
74f331c
 
 
 
 
 
841cdf1
 
 
 
 
 
 
 
 
 
d94e173
 
 
 
 
 
 
 
 
 
 
74f331c
 
841cdf1
74f331c
 
841cdf1
 
 
 
 
 
 
 
d94e173
841cdf1
d94e173
 
841cdf1
d94e173
 
 
 
841cdf1
d94e173
841cdf1
d4ad754
 
74f331c
 
d4ad754
841cdf1
 
74f331c
841cdf1
 
74f331c
 
841cdf1
 
 
74f331c
841cdf1
d4ad754
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import subprocess
import sys
import os

# 1. FORCE STABLE GRADIO 4 + DEPENDENCIES
REQUIRED = ["gradio==4.44.1", "deep-translator==1.11.4", "llama-cpp-python==0.2.90"]
for pkg in REQUIRED:
    try:
        __import__(pkg.split("==")[0].replace("-", "_"))
    except ImportError:
        print(f" Installing {pkg}...")
        subprocess.check_call([sys.executable, "-m", "pip", "install", pkg])

import gradio as gr
from deep_translator import GoogleTranslator
from llama_cpp import Llama

# 2. LOAD POTODOO
print("🥔 Loading Potodoo...")
llm = Llama(
    model_path="Potodoo-V1.Q4_K_M.gguf",  # ✅ MUST MATCH YOUR FILE EXACTLY
    n_ctx=512,
    n_gpu_layers=0,
    n_threads=2,
    verbose=False
)
print("✅ Potodoo ready!")

LANG_MAP = {
    "Português (BR)": "pt",
    "Español": "es",
    "Français": "fr",
    "Italiano": "it",
    "日本語": "ja",
    "한국어": "ko"
}

def respond(message, history, language):
    english_input = message
    target_lang = LANG_MAP.get(language)
    
    if target_lang:
        try:
            t = GoogleTranslator(source=target_lang, target='en')
            english_input = t.translate(message)
        except:
            pass

    messages = [{"role": "system", "content": "You are Potodoo, a helpful AI assistant."}]
    
    if history:
        messages.extend(history[-4:])
        
    messages.append({"role": "user", "content": english_input})
    
    prompt = ""
    for msg in messages:
        prompt += f"<|im_start|>{msg['role']}\n{msg['content']}<|im_end|>\n"
    prompt += "<|im_start|>assistant\n"

    output = llm(prompt, max_tokens=128, stop=["<|im_end|>"], temperature=0.7)
    eng_resp = output['choices'][0]['text'].strip()

    final_resp = eng_resp
    if target_lang:
        try:
            t = GoogleTranslator(source='en', target=target_lang)
            final_resp = t.translate(eng_resp)
        except:
            pass

    return final_resp

# 3. GRADIO 4 UI (Stable, no Node.js proxy)
with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple")) as demo:
    gr.Markdown("# 🥔 Potodoo\n*Multilingual AI Assistant*")
    
    chatbot = gr.Chatbot(height=400)
    
    with gr.Row():
        msg = gr.Textbox(scale=4, show_label=False, placeholder="Type a message...")
        lang = gr.Dropdown(
            choices=["English", "Português (BR)", "Español", "Français", "Italiano", "日本語", "한국어"], 
            value="English", 
            scale=1
        )
    
    msg.submit(respond, [msg, chatbot, lang], [msg, chatbot])
    gr.ClearButton([msg, chatbot], value="Clear Chat")

demo.launch(server_name="0.0.0.0", server_port=7860)