Nhughes09
commited on
Commit
·
94ef460
1
Parent(s):
b438512
Ollama-only chatbot - working locally with llama3.2:3b
Browse files
app.py
CHANGED
|
@@ -1,135 +1,110 @@
|
|
| 1 |
-
# app.py -
|
| 2 |
import gradio as gr
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
|
| 7 |
# ============================================================================
|
| 8 |
-
#
|
| 9 |
# ============================================================================
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# ============================================================================
|
| 14 |
# OLLAMA CONFIGURATION
|
| 15 |
# ============================================================================
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
"llama3.2:3b", # Fast, small
|
| 19 |
-
"gemma3:1b", # Very fast, tiny
|
| 20 |
-
"phi3:mini", # Good quality, medium
|
| 21 |
-
"deepseek-coder:6.7b-instruct-q6_K", # Good for code
|
| 22 |
-
]
|
| 23 |
-
|
| 24 |
-
log_section(logger, "OLLAMA CLIENT SETUP")
|
| 25 |
-
ollama = OllamaClient(logger, model=MODELS[0])
|
| 26 |
|
| 27 |
-
# Check connection
|
| 28 |
-
logger.info("Checking Ollama
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
logger.
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# ============================================================================
|
| 43 |
-
# CHAT
|
| 44 |
# ============================================================================
|
| 45 |
-
def
|
| 46 |
-
"""
|
| 47 |
-
|
| 48 |
-
logger.info(f"
|
| 49 |
-
logger.info(f"History: {len(history)}
|
| 50 |
|
| 51 |
-
# Build
|
| 52 |
-
|
| 53 |
-
for user_msg, bot_msg in history
|
| 54 |
-
|
| 55 |
if bot_msg:
|
| 56 |
-
|
| 57 |
-
|
| 58 |
|
| 59 |
-
|
| 60 |
-
result = ollama.chat(messages)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
# ============================================================================
|
| 69 |
# GRADIO UI
|
| 70 |
# ============================================================================
|
| 71 |
-
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
with gr.Column(scale=4):
|
| 80 |
-
chatbot = gr.Chatbot(height=500, label="Chat")
|
| 81 |
-
msg = gr.Textbox(placeholder="Ask me anything...", label="Your message")
|
| 82 |
-
with gr.Row():
|
| 83 |
-
submit_btn = gr.Button("Send", variant="primary")
|
| 84 |
-
clear = gr.ClearButton([msg, chatbot], value="Clear")
|
| 85 |
-
|
| 86 |
-
with gr.Column(scale=1):
|
| 87 |
-
gr.Markdown("### Status")
|
| 88 |
-
status_box = gr.JSON(
|
| 89 |
-
label="Ollama Stats",
|
| 90 |
-
value=ollama.get_stats()
|
| 91 |
-
)
|
| 92 |
-
model_dropdown = gr.Dropdown(
|
| 93 |
-
choices=ollama.available_models or MODELS,
|
| 94 |
-
value=ollama.model,
|
| 95 |
-
label="Model"
|
| 96 |
-
)
|
| 97 |
-
refresh_btn = gr.Button("Refresh")
|
| 98 |
-
|
| 99 |
-
def user_submit(message, history):
|
| 100 |
-
if not message.strip():
|
| 101 |
-
return "", history, ollama.get_stats()
|
| 102 |
-
return "", history + [[message, None]], ollama.get_stats()
|
| 103 |
-
|
| 104 |
-
def bot_respond(history):
|
| 105 |
-
if not history:
|
| 106 |
-
return history, ollama.get_stats()
|
| 107 |
-
user_message = history[-1][0]
|
| 108 |
-
bot_response = respond(user_message, history[:-1])
|
| 109 |
-
history[-1][1] = bot_response
|
| 110 |
-
return history, ollama.get_stats()
|
| 111 |
-
|
| 112 |
-
def change_model(model):
|
| 113 |
-
ollama.model = model
|
| 114 |
-
logger.info(f"Switched to model: {model}")
|
| 115 |
-
return ollama.get_stats()
|
| 116 |
-
|
| 117 |
-
def refresh_stats():
|
| 118 |
-
ollama.check_connection()
|
| 119 |
-
return ollama.get_stats()
|
| 120 |
-
|
| 121 |
-
msg.submit(user_submit, [msg, chatbot], [msg, chatbot, status_box], queue=False).then(
|
| 122 |
-
bot_respond, chatbot, [chatbot, status_box]
|
| 123 |
-
)
|
| 124 |
-
submit_btn.click(user_submit, [msg, chatbot], [msg, chatbot, status_box], queue=False).then(
|
| 125 |
-
bot_respond, chatbot, [chatbot, status_box]
|
| 126 |
-
)
|
| 127 |
-
model_dropdown.change(change_model, model_dropdown, status_box)
|
| 128 |
-
refresh_btn.click(refresh_stats, outputs=status_box)
|
| 129 |
|
| 130 |
-
|
| 131 |
-
logger.info(
|
| 132 |
-
logger.info("
|
| 133 |
|
| 134 |
if __name__ == "__main__":
|
| 135 |
demo.launch()
|
|
|
|
| 1 |
+
# app.py - Ollama-Only Chatbot
|
| 2 |
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
import logging
|
| 5 |
+
import sys
|
| 6 |
|
| 7 |
# ============================================================================
|
| 8 |
+
# LOGGING SETUP
|
| 9 |
# ============================================================================
|
| 10 |
+
logging.basicConfig(
|
| 11 |
+
level=logging.INFO,
|
| 12 |
+
format="%(asctime)s | %(levelname)-8s | %(message)s",
|
| 13 |
+
handlers=[logging.StreamHandler(sys.stdout)]
|
| 14 |
+
)
|
| 15 |
+
logger = logging.getLogger("ChatbotBrain")
|
| 16 |
+
|
| 17 |
+
logger.info("=" * 60)
|
| 18 |
+
logger.info(" OLLAMA CHATBOT STARTING")
|
| 19 |
+
logger.info("=" * 60)
|
| 20 |
|
| 21 |
# ============================================================================
|
| 22 |
# OLLAMA CONFIGURATION
|
| 23 |
# ============================================================================
|
| 24 |
+
OLLAMA_URL = "http://localhost:11434"
|
| 25 |
+
MODEL = "llama3.2:3b"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# Check Ollama connection
|
| 28 |
+
logger.info(f"Checking Ollama at {OLLAMA_URL}...")
|
| 29 |
+
try:
|
| 30 |
+
response = requests.get(f"{OLLAMA_URL}/api/tags", timeout=5)
|
| 31 |
+
if response.status_code == 200:
|
| 32 |
+
models = [m["name"] for m in response.json().get("models", [])]
|
| 33 |
+
logger.info(f"Ollama connected! Found {len(models)} models")
|
| 34 |
+
if MODEL in models:
|
| 35 |
+
logger.info(f"Using model: {MODEL}")
|
| 36 |
+
else:
|
| 37 |
+
logger.warning(f"Model {MODEL} not found, available: {models[:5]}")
|
| 38 |
+
else:
|
| 39 |
+
logger.error(f"Ollama returned {response.status_code}")
|
| 40 |
+
except Exception as e:
|
| 41 |
+
logger.error(f"Cannot connect to Ollama: {e}")
|
| 42 |
+
logger.error("Run: ollama serve")
|
| 43 |
|
| 44 |
# ============================================================================
|
| 45 |
+
# CHAT FUNCTION
|
| 46 |
# ============================================================================
|
| 47 |
+
def chat_with_ollama(message, history):
|
| 48 |
+
"""Send message to Ollama and get response."""
|
| 49 |
+
logger.info("-" * 40)
|
| 50 |
+
logger.info(f"USER: {message}")
|
| 51 |
+
logger.info(f"History: {len(history)} messages")
|
| 52 |
|
| 53 |
+
# Build prompt from history (tuple format: [(user, bot), ...])
|
| 54 |
+
prompt = "You are a helpful AI assistant.\n\n"
|
| 55 |
+
for user_msg, bot_msg in history:
|
| 56 |
+
prompt += f"User: {user_msg}\n"
|
| 57 |
if bot_msg:
|
| 58 |
+
prompt += f"Assistant: {bot_msg}\n"
|
| 59 |
+
prompt += f"User: {message}\nAssistant:"
|
| 60 |
|
| 61 |
+
logger.info(f"Sending to Ollama ({MODEL})...")
|
|
|
|
| 62 |
|
| 63 |
+
try:
|
| 64 |
+
response = requests.post(
|
| 65 |
+
f"{OLLAMA_URL}/api/generate",
|
| 66 |
+
json={
|
| 67 |
+
"model": MODEL,
|
| 68 |
+
"prompt": prompt,
|
| 69 |
+
"stream": False
|
| 70 |
+
},
|
| 71 |
+
timeout=120
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
if response.status_code == 200:
|
| 75 |
+
result = response.json()
|
| 76 |
+
text = result.get("response", "")
|
| 77 |
+
duration = result.get("total_duration", 0) / 1_000_000_000
|
| 78 |
+
logger.info(f"SUCCESS: Got response in {duration:.1f}s")
|
| 79 |
+
logger.info(f"AI: {text[:100]}...")
|
| 80 |
+
return text.strip()
|
| 81 |
+
else:
|
| 82 |
+
error = f"Ollama returned {response.status_code}: {response.text}"
|
| 83 |
+
logger.error(error)
|
| 84 |
+
return f"Error: {error}"
|
| 85 |
+
|
| 86 |
+
except requests.exceptions.ConnectionError:
|
| 87 |
+
logger.error("Cannot connect to Ollama. Is it running?")
|
| 88 |
+
return "Error: Cannot connect to Ollama. Run: ollama serve"
|
| 89 |
+
except Exception as e:
|
| 90 |
+
logger.error(f"Error: {e}")
|
| 91 |
+
return f"Error: {e}"
|
| 92 |
|
| 93 |
# ============================================================================
|
| 94 |
# GRADIO UI
|
| 95 |
# ============================================================================
|
| 96 |
+
logger.info("Building Gradio UI...")
|
| 97 |
|
| 98 |
+
demo = gr.ChatInterface(
|
| 99 |
+
fn=chat_with_ollama,
|
| 100 |
+
title="CPU Chatbot",
|
| 101 |
+
description=f"**Powered by Ollama** ({MODEL})\n\nUsing local AI - no cloud required!",
|
| 102 |
+
examples=["Hello!", "What is Python?", "Tell me a joke"],
|
| 103 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
logger.info("=" * 60)
|
| 106 |
+
logger.info(" READY - Open http://127.0.0.1:7860")
|
| 107 |
+
logger.info("=" * 60)
|
| 108 |
|
| 109 |
if __name__ == "__main__":
|
| 110 |
demo.launch()
|