Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 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:
|
|
@@ -10,23 +10,21 @@ for pkg in REQUIRED:
|
|
| 10 |
print(f" Installing {pkg}...")
|
| 11 |
subprocess.check_call([sys.executable, "-m", "pip", "install", pkg])
|
| 12 |
|
| 13 |
-
# NOW import normally
|
| 14 |
import gradio as gr
|
| 15 |
from llama_cpp import Llama
|
| 16 |
from deep_translator import GoogleTranslator
|
| 17 |
|
| 18 |
-
#
|
| 19 |
print("🥔 Loading Potodoo...")
|
| 20 |
llm = Llama(
|
| 21 |
-
model_path="potodoo_v1.gguf",
|
| 22 |
-
n_ctx=512,
|
| 23 |
-
n_gpu_layers=0,
|
| 24 |
-
n_threads=2,
|
| 25 |
verbose=False
|
| 26 |
)
|
| 27 |
print("✅ Potodoo ready!")
|
| 28 |
|
| 29 |
-
# Language mapping for translation
|
| 30 |
LANG_MAP = {
|
| 31 |
"Português (BR)": "pt",
|
| 32 |
"Español": "es",
|
|
@@ -36,28 +34,27 @@ LANG_MAP = {
|
|
| 36 |
"한국어": "ko"
|
| 37 |
}
|
| 38 |
|
| 39 |
-
def
|
| 40 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
chat_history = []
|
| 42 |
if history:
|
| 43 |
for turn in history:
|
| 44 |
chat_history.append({"role": "user", "content": turn[0]})
|
| 45 |
chat_history.append({"role": "assistant", "content": turn[1]})
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
target_lang_code = LANG_MAP.get(language)
|
| 50 |
-
|
| 51 |
-
if target_lang_code:
|
| 52 |
-
try:
|
| 53 |
-
translator = GoogleTranslator(source=target_lang_code, target='en')
|
| 54 |
-
english_input = translator.translate(message)
|
| 55 |
-
except Exception as e:
|
| 56 |
-
print(f"Input translation error: {e}")
|
| 57 |
-
|
| 58 |
-
# Build Prompt with System Instruction
|
| 59 |
-
messages = [{"role": "system", "content": "You are Potodoo, a helpful and quirky AI assistant."}]
|
| 60 |
-
messages.extend(chat_history[-4:]) # Keep last 4 turns to save memory
|
| 61 |
messages.append({"role": "user", "content": english_input})
|
| 62 |
|
| 63 |
prompt = ""
|
|
@@ -65,29 +62,24 @@ def chat(message, history, language):
|
|
| 65 |
prompt += f"<|im_start|>{msg['role']}\n{msg['content']}<|im_end|>\n"
|
| 66 |
prompt += "<|im_start|>assistant\n"
|
| 67 |
|
| 68 |
-
# Generate
|
| 69 |
output = llm(prompt, max_tokens=128, stop=["<|im_end|>"], temperature=0.7)
|
| 70 |
-
|
| 71 |
|
| 72 |
-
# Translate
|
| 73 |
-
|
| 74 |
-
if
|
| 75 |
try:
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
except
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
return final_response
|
| 82 |
|
| 83 |
-
|
| 84 |
-
bot_message = chat(message, chat_history, language)
|
| 85 |
-
chat_history.append((message, bot_message))
|
| 86 |
-
return "", chat_history
|
| 87 |
|
| 88 |
-
#
|
| 89 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple")) as demo:
|
| 90 |
-
gr.Markdown("#
|
| 91 |
|
| 92 |
chatbot = gr.Chatbot(height=400, type="tuples")
|
| 93 |
|
|
@@ -96,12 +88,10 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple")) as demo:
|
|
| 96 |
lang = gr.Dropdown(
|
| 97 |
choices=["English", "Português (BR)", "Español", "Français", "Italiano", "日本語", "한국어"],
|
| 98 |
value="English",
|
| 99 |
-
scale=1
|
| 100 |
-
label="Language"
|
| 101 |
)
|
| 102 |
|
| 103 |
msg.submit(respond, [msg, chatbot, lang], [msg, chatbot])
|
| 104 |
gr.ClearButton([msg, chatbot], value="Clear Chat")
|
| 105 |
|
| 106 |
-
|
| 107 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 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:
|
|
|
|
| 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 |
+
# Load Potodoo
|
| 18 |
print("🥔 Loading Potodoo...")
|
| 19 |
llm = Llama(
|
| 20 |
+
model_path="potodoo_v1.gguf",
|
| 21 |
+
n_ctx=512,
|
| 22 |
+
n_gpu_layers=0,
|
| 23 |
+
n_threads=2,
|
| 24 |
verbose=False
|
| 25 |
)
|
| 26 |
print("✅ Potodoo ready!")
|
| 27 |
|
|
|
|
| 28 |
LANG_MAP = {
|
| 29 |
"Português (BR)": "pt",
|
| 30 |
"Español": "es",
|
|
|
|
| 34 |
"한국어": "ko"
|
| 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 |
+
|
| 42 |
+
if target_lang:
|
| 43 |
+
try:
|
| 44 |
+
t = GoogleTranslator(source=target_lang, target='en')
|
| 45 |
+
english_input = t.translate(message)
|
| 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, a helpful AI."}]
|
| 57 |
+
messages.extend(chat_history[-4:])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
messages.append({"role": "user", "content": english_input})
|
| 59 |
|
| 60 |
prompt = ""
|
|
|
|
| 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:
|
| 73 |
+
t = GoogleTranslator(source='en', target=target_lang)
|
| 74 |
+
final_resp = t.translate(eng_resp)
|
| 75 |
+
except:
|
| 76 |
+
pass
|
|
|
|
|
|
|
| 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\n*Multilingual AI Assistant*")
|
| 83 |
|
| 84 |
chatbot = gr.Chatbot(height=400, type="tuples")
|
| 85 |
|
|
|
|
| 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 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|