try and switch to llm library
Browse files- .huggingface-space +9 -0
- app.py +100 -30
- llm_interface.py +1 -1
- requirements.txt +4 -0
- setup.sh +39 -0
.huggingface-space
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
title: AAC Knowledge Graph Demo
|
2 |
+
emoji: 💬
|
3 |
+
colorFrom: blue
|
4 |
+
colorTo: green
|
5 |
+
sdk: gradio
|
6 |
+
sdk_version: 4.19.2
|
7 |
+
app_file: app.py
|
8 |
+
pinned: false
|
9 |
+
license: mit
|
app.py
CHANGED
@@ -2,28 +2,72 @@ import gradio as gr
|
|
2 |
import whisper
|
3 |
import random
|
4 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
from utils import SocialGraphManager
|
6 |
from llm_interface import LLMInterface
|
7 |
|
8 |
-
# Define available models
|
9 |
AVAILABLE_MODELS = {
|
10 |
# Gemini models (online API)
|
11 |
-
"gemini-1.5-flash-latest": "🌐 Gemini 1.5 Flash (Online API - Fast,
|
12 |
-
"gemini-
|
13 |
-
|
14 |
-
"gpt-3.5-turbo": "🌐 ChatGPT 3.5 (Online API)",
|
15 |
-
"gpt-4o-mini": "🌐 GPT-4o Mini (Online API - Fast)",
|
16 |
-
# Ollama models (if installed locally)
|
17 |
-
"ollama/gemma:7b": "💻 Gemma 7B (Offline - requires Ollama)",
|
18 |
-
"ollama/llama3:8b": "💻 Llama 3 8B (Offline - requires Ollama)",
|
19 |
}
|
20 |
|
21 |
# Initialize the social graph manager
|
22 |
social_graph = SocialGraphManager("social_graph.json")
|
23 |
|
24 |
-
#
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
# Test the model to make sure it's working
|
29 |
print("Testing model connection...")
|
@@ -145,7 +189,7 @@ def change_model(model_name, progress=gr.Progress()):
|
|
145 |
Returns:
|
146 |
A status message about the model change
|
147 |
"""
|
148 |
-
global suggestion_generator
|
149 |
|
150 |
print(f"Changing model to: {model_name}")
|
151 |
|
@@ -156,24 +200,50 @@ def change_model(model_name, progress=gr.Progress()):
|
|
156 |
# Show progress indicator
|
157 |
progress(0, desc=f"Loading model: {model_name}")
|
158 |
|
159 |
-
# Create a new LLMInterface with the selected model
|
160 |
try:
|
161 |
progress(0.3, desc=f"Initializing {model_name}...")
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
else:
|
175 |
-
|
176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
except Exception as e:
|
178 |
print(f"Error changing model: {e}")
|
179 |
progress(1.0, desc="Error loading model")
|
@@ -576,9 +646,9 @@ with gr.Blocks(title="Will's AAC Communication Aid", css="custom.css") as demo:
|
|
576 |
with gr.Row():
|
577 |
model_dropdown = gr.Dropdown(
|
578 |
choices=list(AVAILABLE_MODELS.keys()),
|
579 |
-
value="gemini-1.5-flash-latest",
|
580 |
label="Language Model",
|
581 |
-
info="Select which AI model to use (
|
582 |
)
|
583 |
|
584 |
temperature_slider = gr.Slider(
|
|
|
2 |
import whisper
|
3 |
import random
|
4 |
import time
|
5 |
+
import os
|
6 |
+
import subprocess
|
7 |
+
import warnings
|
8 |
+
|
9 |
+
# Set environment variable to avoid tokenizer warnings
|
10 |
+
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
11 |
+
|
12 |
from utils import SocialGraphManager
|
13 |
from llm_interface import LLMInterface
|
14 |
|
15 |
+
# Define available models - using only the ones specified by the user
|
16 |
AVAILABLE_MODELS = {
|
17 |
# Gemini models (online API)
|
18 |
+
"gemini-1.5-flash-8b-latest": "🌐 Gemini 1.5 Flash 8B (Online API - Fast, Cheapest)",
|
19 |
+
"gemini-2.0-flash": "🌐 Gemini 2.0 Flash (Online API - Better quality)",
|
20 |
+
"gemma-3-27b-it": "🌐 Gemma 3 27B-IT (Online API - High quality)",
|
|
|
|
|
|
|
|
|
|
|
21 |
}
|
22 |
|
23 |
# Initialize the social graph manager
|
24 |
social_graph = SocialGraphManager("social_graph.json")
|
25 |
|
26 |
+
# Check if we're running on Hugging Face Spaces
|
27 |
+
is_huggingface_spaces = "SPACE_ID" in os.environ
|
28 |
+
|
29 |
+
# Print environment info for debugging
|
30 |
+
print(f"Running on Hugging Face Spaces: {is_huggingface_spaces}")
|
31 |
+
print(f"GEMINI_API_KEY set: {'Yes' if os.environ.get('GEMINI_API_KEY') else 'No'}")
|
32 |
+
print(f"HF_TOKEN set: {'Yes' if os.environ.get('HF_TOKEN') else 'No'}")
|
33 |
+
|
34 |
+
# Try to run the setup script if we're on Hugging Face Spaces
|
35 |
+
if is_huggingface_spaces:
|
36 |
+
try:
|
37 |
+
print("Running setup script...")
|
38 |
+
subprocess.run(["bash", "setup.sh"], check=True)
|
39 |
+
print("Setup script completed successfully")
|
40 |
+
except Exception as e:
|
41 |
+
print(f"Error running setup script: {e}")
|
42 |
+
|
43 |
+
# Check if LLM tool is installed
|
44 |
+
llm_installed = False
|
45 |
+
try:
|
46 |
+
result = subprocess.run(
|
47 |
+
["llm", "--version"],
|
48 |
+
capture_output=True,
|
49 |
+
text=True,
|
50 |
+
timeout=5,
|
51 |
+
)
|
52 |
+
if result.returncode == 0:
|
53 |
+
print(f"LLM tool is installed: {result.stdout.strip()}")
|
54 |
+
llm_installed = True
|
55 |
+
else:
|
56 |
+
print("LLM tool returned an error.")
|
57 |
+
except Exception as e:
|
58 |
+
print(f"LLM tool not available: {e}")
|
59 |
+
|
60 |
+
# Initialize the suggestion generator
|
61 |
+
if llm_installed:
|
62 |
+
print("Initializing with Gemini 1.5 Flash 8B (online model via LLM tool)")
|
63 |
+
suggestion_generator = LLMInterface("gemini-1.5-flash-8b-latest")
|
64 |
+
use_llm_interface = True
|
65 |
+
else:
|
66 |
+
print("LLM tool not available, falling back to direct Hugging Face implementation")
|
67 |
+
from utils import SuggestionGenerator
|
68 |
+
|
69 |
+
suggestion_generator = SuggestionGenerator("google/gemma-3-1b-it")
|
70 |
+
use_llm_interface = False
|
71 |
|
72 |
# Test the model to make sure it's working
|
73 |
print("Testing model connection...")
|
|
|
189 |
Returns:
|
190 |
A status message about the model change
|
191 |
"""
|
192 |
+
global suggestion_generator, use_llm_interface
|
193 |
|
194 |
print(f"Changing model to: {model_name}")
|
195 |
|
|
|
200 |
# Show progress indicator
|
201 |
progress(0, desc=f"Loading model: {model_name}")
|
202 |
|
|
|
203 |
try:
|
204 |
progress(0.3, desc=f"Initializing {model_name}...")
|
205 |
+
|
206 |
+
# Use the appropriate interface based on what's available
|
207 |
+
if use_llm_interface:
|
208 |
+
# Create a new LLMInterface with the selected model
|
209 |
+
new_generator = LLMInterface(model_name)
|
210 |
+
|
211 |
+
# Test if the model works
|
212 |
+
progress(0.6, desc="Testing model connection...")
|
213 |
+
test_result = new_generator.test_model()
|
214 |
+
print(f"Model test result: {test_result}")
|
215 |
+
|
216 |
+
if new_generator.model_loaded:
|
217 |
+
# Replace the current generator with the new one
|
218 |
+
suggestion_generator = new_generator
|
219 |
+
progress(1.0, desc=f"Model loaded: {model_name}")
|
220 |
+
return f"Successfully switched to model: {model_name}"
|
221 |
+
else:
|
222 |
+
progress(1.0, desc="Model loading failed")
|
223 |
+
return (
|
224 |
+
f"Failed to load model: {model_name}. Using previous model instead."
|
225 |
+
)
|
226 |
else:
|
227 |
+
# Using direct Hugging Face implementation
|
228 |
+
from utils import SuggestionGenerator
|
229 |
+
|
230 |
+
# Create a new SuggestionGenerator with the selected model
|
231 |
+
new_generator = SuggestionGenerator(model_name)
|
232 |
+
|
233 |
+
# Test if the model works
|
234 |
+
progress(0.6, desc="Testing model connection...")
|
235 |
+
success = new_generator.load_model(model_name)
|
236 |
+
|
237 |
+
if success:
|
238 |
+
# Replace the current generator with the new one
|
239 |
+
suggestion_generator = new_generator
|
240 |
+
progress(1.0, desc=f"Model loaded: {model_name}")
|
241 |
+
return f"Successfully switched to model: {model_name}"
|
242 |
+
else:
|
243 |
+
progress(1.0, desc="Model loading failed")
|
244 |
+
return (
|
245 |
+
f"Failed to load model: {model_name}. Using previous model instead."
|
246 |
+
)
|
247 |
except Exception as e:
|
248 |
print(f"Error changing model: {e}")
|
249 |
progress(1.0, desc="Error loading model")
|
|
|
646 |
with gr.Row():
|
647 |
model_dropdown = gr.Dropdown(
|
648 |
choices=list(AVAILABLE_MODELS.keys()),
|
649 |
+
value="gemini-1.5-flash-8b-latest",
|
650 |
label="Language Model",
|
651 |
+
info="Select which AI model to use (all are online API models)",
|
652 |
)
|
653 |
|
654 |
temperature_slider = gr.Slider(
|
llm_interface.py
CHANGED
@@ -225,7 +225,7 @@ My conversation starter to {name}:"""
|
|
225 |
],
|
226 |
capture_output=True,
|
227 |
text=True,
|
228 |
-
timeout=
|
229 |
)
|
230 |
|
231 |
if progress_callback:
|
|
|
225 |
],
|
226 |
capture_output=True,
|
227 |
text=True,
|
228 |
+
timeout=30, # Increase timeout for Gemini API calls
|
229 |
)
|
230 |
|
231 |
if progress_callback:
|
requirements.txt
CHANGED
@@ -7,3 +7,7 @@ openai-whisper>=20231117
|
|
7 |
bitsandbytes>=0.41.0
|
8 |
accelerate>=0.21.0
|
9 |
google-generativeai>=0.3.0
|
|
|
|
|
|
|
|
|
|
7 |
bitsandbytes>=0.41.0
|
8 |
accelerate>=0.21.0
|
9 |
google-generativeai>=0.3.0
|
10 |
+
llm>=0.25.0
|
11 |
+
llm-gemini>=0.1.0
|
12 |
+
llm-openai>=0.1.0
|
13 |
+
llm-ollama>=0.1.0
|
setup.sh
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
|
3 |
+
# Install Simon Willison's LLM library and plugins
|
4 |
+
pip install llm
|
5 |
+
pip install llm-gemini
|
6 |
+
pip install llm-openai
|
7 |
+
pip install llm-ollama
|
8 |
+
|
9 |
+
# Set up environment variables
|
10 |
+
echo "Setting up environment variables..."
|
11 |
+
if [ -n "$GEMINI_API_KEY" ]; then
|
12 |
+
echo "GEMINI_API_KEY is set"
|
13 |
+
# Configure LLM to use Gemini
|
14 |
+
llm keys set gemini "$GEMINI_API_KEY"
|
15 |
+
else
|
16 |
+
echo "GEMINI_API_KEY is not set"
|
17 |
+
fi
|
18 |
+
|
19 |
+
if [ -n "$OPENAI_API_KEY" ]; then
|
20 |
+
echo "OPENAI_API_KEY is set"
|
21 |
+
# Configure LLM to use OpenAI
|
22 |
+
llm keys set openai "$OPENAI_API_KEY"
|
23 |
+
else
|
24 |
+
echo "OPENAI_API_KEY is not set"
|
25 |
+
fi
|
26 |
+
|
27 |
+
if [ -n "$HF_TOKEN" ]; then
|
28 |
+
echo "HF_TOKEN is set"
|
29 |
+
# Configure Hugging Face token
|
30 |
+
huggingface-cli login --token "$HF_TOKEN"
|
31 |
+
else
|
32 |
+
echo "HF_TOKEN is not set"
|
33 |
+
fi
|
34 |
+
|
35 |
+
# List available models
|
36 |
+
echo "Available LLM models:"
|
37 |
+
llm models
|
38 |
+
|
39 |
+
echo "Setup complete!"
|