Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,50 @@
|
|
| 1 |
import subprocess
|
| 2 |
import sys
|
|
|
|
| 3 |
|
| 4 |
-
# Self-install dependencies
|
| 5 |
REQUIRED = ["gradio==4.44.1", "deep-translator==1.11.4", "llama-cpp-python==0.2.90"]
|
| 6 |
for pkg in REQUIRED:
|
| 7 |
try:
|
| 8 |
__import__(pkg.split("==")[0])
|
| 9 |
except ImportError:
|
| 10 |
print(f" Installing {pkg}...")
|
| 11 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", pkg])
|
| 12 |
|
| 13 |
import gradio as gr
|
| 14 |
-
from llama_cpp import Llama
|
| 15 |
from deep_translator import GoogleTranslator
|
| 16 |
|
| 17 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
print("🥔 Loading Potodoo...")
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
LANG_MAP = {
|
| 29 |
"Português (BR)": "pt",
|
|
@@ -35,7 +56,6 @@ LANG_MAP = {
|
|
| 35 |
}
|
| 36 |
|
| 37 |
def respond(message, history, language):
|
| 38 |
-
# Translate input to English
|
| 39 |
english_input = message
|
| 40 |
target_lang = LANG_MAP.get(language)
|
| 41 |
|
|
@@ -46,14 +66,13 @@ def respond(message, history, language):
|
|
| 46 |
except:
|
| 47 |
pass
|
| 48 |
|
| 49 |
-
# Build prompt
|
| 50 |
chat_history = []
|
| 51 |
if history:
|
| 52 |
for turn in history:
|
| 53 |
chat_history.append({"role": "user", "content": turn[0]})
|
| 54 |
chat_history.append({"role": "assistant", "content": turn[1]})
|
| 55 |
|
| 56 |
-
messages = [{"role": "system", "content": "You are Potodoo
|
| 57 |
messages.extend(chat_history[-4:])
|
| 58 |
messages.append({"role": "user", "content": english_input})
|
| 59 |
|
|
@@ -62,11 +81,9 @@ def respond(message, history, language):
|
|
| 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,21 +94,18 @@ def respond(message, history, language):
|
|
| 77 |
|
| 78 |
return final_resp
|
| 79 |
|
| 80 |
-
# UI
|
| 81 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple")) as demo:
|
| 82 |
-
gr.Markdown("# 🥔 Potodoo
|
| 83 |
-
|
| 84 |
chatbot = gr.Chatbot(height=400, type="tuples")
|
| 85 |
|
| 86 |
with gr.Row():
|
| 87 |
-
msg = gr.Textbox(scale=4, show_label=False, placeholder="Type
|
| 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]
|
| 96 |
|
| 97 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import subprocess
|
| 2 |
import sys
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# Self-install dependencies with error handling
|
| 6 |
REQUIRED = ["gradio==4.44.1", "deep-translator==1.11.4", "llama-cpp-python==0.2.90"]
|
| 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", "--no-cache-dir", pkg])
|
| 13 |
|
| 14 |
import gradio as gr
|
|
|
|
| 15 |
from deep_translator import GoogleTranslator
|
| 16 |
|
| 17 |
+
# Check if llama_cpp imported successfully
|
| 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.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 |
+
try:
|
| 37 |
+
llm = Llama(
|
| 38 |
+
model_path=MODEL_PATH,
|
| 39 |
+
n_ctx=512,
|
| 40 |
+
n_gpu_layers=0,
|
| 41 |
+
n_threads=2,
|
| 42 |
+
verbose=True # Enable verbose to see actual errors
|
| 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 |
}
|
| 57 |
|
| 58 |
def respond(message, history, language):
|
|
|
|
| 59 |
english_input = message
|
| 60 |
target_lang = LANG_MAP.get(language)
|
| 61 |
|
|
|
|
| 66 |
except:
|
| 67 |
pass
|
| 68 |
|
|
|
|
| 69 |
chat_history = []
|
| 70 |
if history:
|
| 71 |
for turn in history:
|
| 72 |
chat_history.append({"role": "user", "content": turn[0]})
|
| 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 |
|
|
|
|
| 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 |
|
| 95 |
return final_resp
|
| 96 |
|
|
|
|
| 97 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple")) as demo:
|
| 98 |
+
gr.Markdown("# 🥔 Potodoo")
|
|
|
|
| 99 |
chatbot = gr.Chatbot(height=400, type="tuples")
|
| 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", scale=1
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
msg.submit(respond, [msg, chatbot, lang], [msg, chatbot])
|
| 109 |
+
gr.ClearButton([msg, chatbot])
|
| 110 |
|
| 111 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|