OttoCapi commited on
Commit
d4ad754
·
verified ·
1 Parent(s): 0fc172f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -15
app.py CHANGED
@@ -2,8 +2,8 @@ import subprocess
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("-", "_"))
@@ -15,10 +15,10 @@ import gradio as gr
15
  from deep_translator import GoogleTranslator
16
  from llama_cpp import Llama
17
 
18
- # Load Potodoo
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,
@@ -36,7 +36,6 @@ LANG_MAP = {
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,26 +46,21 @@ def respond(message, history, language):
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,11 +71,11 @@ def respond(message, history, language):
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...")
@@ -94,5 +88,4 @@ with gr.Blocks() as demo:
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"))
 
2
  import sys
3
  import os
4
 
5
+ # 1. FORCE STABLE GRADIO 4 + DEPENDENCIES
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].replace("-", "_"))
 
15
  from deep_translator import GoogleTranslator
16
  from llama_cpp import Llama
17
 
18
+ # 2. LOAD POTODOO
19
  print("🥔 Loading Potodoo...")
20
  llm = Llama(
21
+ model_path="Potodoo-V1.Q4_K_M.gguf", # ✅ MUST MATCH YOUR FILE EXACTLY
22
  n_ctx=512,
23
  n_gpu_layers=0,
24
  n_threads=2,
 
36
  }
37
 
38
  def respond(message, history, language):
 
39
  english_input = message
40
  target_lang = LANG_MAP.get(language)
41
 
 
46
  except:
47
  pass
48
 
 
49
  messages = [{"role": "system", "content": "You are Potodoo, a helpful AI assistant."}]
50
 
 
51
  if history:
52
  messages.extend(history[-4:])
53
 
54
  messages.append({"role": "user", "content": english_input})
55
 
 
56
  prompt = ""
57
  for msg in messages:
58
  prompt += f"<|im_start|>{msg['role']}\n{msg['content']}<|im_end|>\n"
59
  prompt += "<|im_start|>assistant\n"
60
 
 
61
  output = llm(prompt, max_tokens=128, stop=["<|im_end|>"], temperature=0.7)
62
  eng_resp = output['choices'][0]['text'].strip()
63
 
 
64
  final_resp = eng_resp
65
  if target_lang:
66
  try:
 
71
 
72
  return final_resp
73
 
74
+ # 3. GRADIO 4 UI (Stable, no Node.js proxy)
75
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple")) as demo:
76
  gr.Markdown("# 🥔 Potodoo\n*Multilingual AI Assistant*")
77
 
78
+ chatbot = gr.Chatbot(height=400)
79
 
80
  with gr.Row():
81
  msg = gr.Textbox(scale=4, show_label=False, placeholder="Type a message...")
 
88
  msg.submit(respond, [msg, chatbot, lang], [msg, chatbot])
89
  gr.ClearButton([msg, chatbot], value="Clear Chat")
90
 
91
+ demo.launch(server_name="0.0.0.0", server_port=7860)