Update app.py
Browse files
app.py
CHANGED
|
@@ -2,49 +2,29 @@ import subprocess
|
|
| 2 |
import sys
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# Self-install dependencies
|
| 6 |
-
REQUIRED = ["
|
| 7 |
for pkg in REQUIRED:
|
| 8 |
try:
|
| 9 |
-
__import__(pkg.split("==")[0])
|
| 10 |
except ImportError:
|
| 11 |
print(f" Installing {pkg}...")
|
| 12 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install",
|
| 13 |
|
| 14 |
import gradio as gr
|
| 15 |
from deep_translator import GoogleTranslator
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
try:
|
| 19 |
-
from llama_cpp import Llama
|
| 20 |
-
except ImportError as e:
|
| 21 |
-
print(f" FATAL: llama_cpp failed to import: {e}")
|
| 22 |
-
print(" This usually means it was compiled without AVX2 support.")
|
| 23 |
-
print(" Trying to reinstall with CMAKE_ARGS...")
|
| 24 |
-
os.environ["CMAKE_ARGS"] = "-DLLAMA_BLAS=OFF -DLLAMA_AVX2=ON"
|
| 25 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", "--no-cache-dir", "--force-reinstall", "llama-cpp-python==0.2.90"])
|
| 26 |
-
from llama_cpp import Llama
|
| 27 |
-
|
| 28 |
-
# Verify model file exists
|
| 29 |
-
MODEL_PATH = "Potodoo-V1.Q4_K_M.gguf"
|
| 30 |
-
if not os.path.exists(MODEL_PATH):
|
| 31 |
-
print(f" FATAL: Model file '{MODEL_PATH}' not found!")
|
| 32 |
-
print(f" Files in /app: {os.listdir('/app')}")
|
| 33 |
-
raise FileNotFoundError(f"Model file missing: {MODEL_PATH}")
|
| 34 |
-
|
| 35 |
print("🥔 Loading Potodoo...")
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
print("✅ Potodoo ready!")
|
| 45 |
-
except Exception as e:
|
| 46 |
-
print(f" FATAL: Failed to load model: {e}")
|
| 47 |
-
raise
|
| 48 |
|
| 49 |
LANG_MAP = {
|
| 50 |
"Português (BR)": "pt",
|
|
@@ -56,6 +36,7 @@ LANG_MAP = {
|
|
| 56 |
}
|
| 57 |
|
| 58 |
def respond(message, history, language):
|
|
|
|
| 59 |
english_input = message
|
| 60 |
target_lang = LANG_MAP.get(language)
|
| 61 |
|
|
@@ -66,24 +47,26 @@ def respond(message, history, language):
|
|
| 66 |
except:
|
| 67 |
pass
|
| 68 |
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
| 70 |
if history:
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
chat_history.append({"role": "assistant", "content": turn[1]})
|
| 74 |
-
|
| 75 |
-
messages = [{"role": "system", "content": "You are Potodoo."}]
|
| 76 |
-
messages.extend(chat_history[-4:])
|
| 77 |
messages.append({"role": "user", "content": english_input})
|
| 78 |
|
|
|
|
| 79 |
prompt = ""
|
| 80 |
for msg in messages:
|
| 81 |
prompt += f"<|im_start|>{msg['role']}\n{msg['content']}<|im_end|>\n"
|
| 82 |
prompt += "<|im_start|>assistant\n"
|
| 83 |
|
|
|
|
| 84 |
output = llm(prompt, max_tokens=128, stop=["<|im_end|>"], temperature=0.7)
|
| 85 |
eng_resp = output['choices'][0]['text'].strip()
|
| 86 |
|
|
|
|
| 87 |
final_resp = eng_resp
|
| 88 |
if target_lang:
|
| 89 |
try:
|
|
@@ -94,18 +77,22 @@ def respond(message, history, language):
|
|
| 94 |
|
| 95 |
return final_resp
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
| 100 |
|
| 101 |
with gr.Row():
|
| 102 |
-
msg = gr.Textbox(scale=4, show_label=False, placeholder="Type...")
|
| 103 |
lang = gr.Dropdown(
|
| 104 |
choices=["English", "Português (BR)", "Español", "Français", "Italiano", "日本語", "한국어"],
|
| 105 |
-
value="English",
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
msg.submit(respond, [msg, chatbot, lang], [msg, chatbot])
|
| 109 |
-
gr.ClearButton([msg, chatbot])
|
| 110 |
|
| 111 |
-
|
|
|
|
|
|
| 2 |
import sys
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Self-install dependencies (Gradio is already installed by HF)
|
| 6 |
+
REQUIRED = ["deep-translator==1.11.4", "llama-cpp-python==0.2.90"]
|
| 7 |
for pkg in REQUIRED:
|
| 8 |
try:
|
| 9 |
+
__import__(pkg.split("==")[0].replace("-", "_"))
|
| 10 |
except ImportError:
|
| 11 |
print(f" Installing {pkg}...")
|
| 12 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", pkg])
|
| 13 |
|
| 14 |
import gradio as gr
|
| 15 |
from deep_translator import GoogleTranslator
|
| 16 |
+
from llama_cpp import Llama
|
| 17 |
|
| 18 |
+
# Load Potodoo (UPDATED FILENAME!)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
print("🥔 Loading Potodoo...")
|
| 20 |
+
llm = Llama(
|
| 21 |
+
model_path="Potodoo-V1.Q4_K_M.gguf",
|
| 22 |
+
n_ctx=512,
|
| 23 |
+
n_gpu_layers=0,
|
| 24 |
+
n_threads=2,
|
| 25 |
+
verbose=False
|
| 26 |
+
)
|
| 27 |
+
print("✅ Potodoo ready!")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
LANG_MAP = {
|
| 30 |
"Português (BR)": "pt",
|
|
|
|
| 36 |
}
|
| 37 |
|
| 38 |
def respond(message, history, language):
|
| 39 |
+
# Gradio 6.0 passes history as a list of dicts: [{"role": "user", "content": "..."}]
|
| 40 |
english_input = message
|
| 41 |
target_lang = LANG_MAP.get(language)
|
| 42 |
|
|
|
|
| 47 |
except:
|
| 48 |
pass
|
| 49 |
|
| 50 |
+
# Build prompt
|
| 51 |
+
messages = [{"role": "system", "content": "You are Potodoo, a helpful AI assistant."}]
|
| 52 |
+
|
| 53 |
+
# Add history (last 4 turns to save memory)
|
| 54 |
if history:
|
| 55 |
+
messages.extend(history[-4:])
|
| 56 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
messages.append({"role": "user", "content": english_input})
|
| 58 |
|
| 59 |
+
# Format as ChatML for SmolLM2
|
| 60 |
prompt = ""
|
| 61 |
for msg in messages:
|
| 62 |
prompt += f"<|im_start|>{msg['role']}\n{msg['content']}<|im_end|>\n"
|
| 63 |
prompt += "<|im_start|>assistant\n"
|
| 64 |
|
| 65 |
+
# Generate
|
| 66 |
output = llm(prompt, max_tokens=128, stop=["<|im_end|>"], temperature=0.7)
|
| 67 |
eng_resp = output['choices'][0]['text'].strip()
|
| 68 |
|
| 69 |
+
# Translate back
|
| 70 |
final_resp = eng_resp
|
| 71 |
if target_lang:
|
| 72 |
try:
|
|
|
|
| 77 |
|
| 78 |
return final_resp
|
| 79 |
|
| 80 |
+
# Gradio 6.0 UI (No 'type' or 'theme' arguments here!)
|
| 81 |
+
with gr.Blocks() as demo:
|
| 82 |
+
gr.Markdown("# 🥔 Potodoo\n*Multilingual AI Assistant*")
|
| 83 |
+
|
| 84 |
+
chatbot = gr.Chatbot(height=400) # ✅ Default 'messages' format in Gradio 6
|
| 85 |
|
| 86 |
with gr.Row():
|
| 87 |
+
msg = gr.Textbox(scale=4, show_label=False, placeholder="Type a message...")
|
| 88 |
lang = gr.Dropdown(
|
| 89 |
choices=["English", "Português (BR)", "Español", "Français", "Italiano", "日本語", "한국어"],
|
| 90 |
+
value="English",
|
| 91 |
+
scale=1
|
| 92 |
)
|
| 93 |
|
| 94 |
msg.submit(respond, [msg, chatbot, lang], [msg, chatbot])
|
| 95 |
+
gr.ClearButton([msg, chatbot], value="Clear Chat")
|
| 96 |
|
| 97 |
+
# ✅ Theme is passed to launch() in Gradio 6.0!
|
| 98 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, theme=gr.themes.Soft(primary_hue="purple"))
|