Nhughes09 commited on
Commit
94ef460
·
1 Parent(s): b438512

Ollama-only chatbot - working locally with llama3.2:3b

Browse files
Files changed (1) hide show
  1. app.py +85 -110
app.py CHANGED
@@ -1,135 +1,110 @@
1
- # app.py - Main Gradio Application with Ollama Backend
2
  import gradio as gr
3
- import os
4
- from logging_config import setup_logging, log_banner, log_section, log_startup_info
5
- from ollama_client import OllamaClient
6
 
7
  # ============================================================================
8
- # INITIALIZATION
9
  # ============================================================================
10
- logger = setup_logging()
11
- log_startup_info(logger)
 
 
 
 
 
 
 
 
12
 
13
  # ============================================================================
14
  # OLLAMA CONFIGURATION
15
  # ============================================================================
16
- # Models to try in order of preference (smaller = faster, more reliable)
17
- MODELS = [
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 and find working model
28
- logger.info("Checking Ollama connection...")
29
- if ollama.check_connection():
30
- logger.info("Ollama is running!")
31
- available = ollama.list_models()
32
-
33
- # Find first available preferred model
34
- for model in MODELS:
35
- if model in available:
36
- ollama.model = model
37
- logger.info(f"Selected model: {model}")
38
- break
39
- else:
40
- logger.warning("Ollama not available - running in limited mode")
 
 
41
 
42
  # ============================================================================
43
- # CHAT RESPONSE FUNCTION
44
  # ============================================================================
45
- def respond(message, history):
46
- """Generate AI response using Ollama."""
47
- log_section(logger, "NEW USER MESSAGE")
48
- logger.info(f"User: {message}")
49
- logger.info(f"History: {len(history)} previous messages")
50
 
51
- # Build messages array
52
- messages = [{"role": "system", "content": "You are a helpful AI assistant. Be concise and helpful."}]
53
- for user_msg, bot_msg in history[-5:]: # Last 5 exchanges for context
54
- messages.append({"role": "user", "content": user_msg})
55
  if bot_msg:
56
- messages.append({"role": "assistant", "content": bot_msg})
57
- messages.append({"role": "user", "content": message})
58
 
59
- # Call Ollama
60
- result = ollama.chat(messages)
61
 
62
- if result["success"]:
63
- return result["response"].strip()
64
- else:
65
- error_msg = result.get("error", "Unknown error")
66
- return f"Error: {error_msg}\n\nMake sure Ollama is running: `ollama serve`"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
  # ============================================================================
69
  # GRADIO UI
70
  # ============================================================================
71
- log_section(logger, "BUILDING GRADIO UI")
72
 
73
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
74
- gr.Markdown("# CPU Chatbot")
75
- gr.Markdown(f"### Powered by Ollama ({ollama.model})")
76
- gr.Markdown("*Using local AI - no cloud required!*")
77
-
78
- with gr.Row():
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
- log_banner(logger, "SYSTEM READY - USING OLLAMA")
131
- logger.info(f"Model: {ollama.model}")
132
- logger.info("Run 'ollama serve' if not already running")
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()