Spaces:
Sleeping
Sleeping
Update Agentic Browser for Hugging Face Spaces
Browse files- app.py +23 -5
- deploy.py +116 -0
- health_check.py +63 -0
- src/streamlit_app.py +133 -40
app.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
-
Main entry point for the Agentic Browser application.
|
| 4 |
"""
|
| 5 |
import os
|
| 6 |
import sys
|
|
|
|
| 7 |
|
| 8 |
# Add the current directory to the Python path
|
| 9 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
@@ -15,8 +16,25 @@ src_dir = os.path.join(current_dir, 'src')
|
|
| 15 |
if src_dir not in sys.path:
|
| 16 |
sys.path.insert(0, src_dir)
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
+
Main entry point for the Agentic Browser application on Hugging Face Spaces.
|
| 4 |
"""
|
| 5 |
import os
|
| 6 |
import sys
|
| 7 |
+
import streamlit as st
|
| 8 |
|
| 9 |
# Add the current directory to the Python path
|
| 10 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
| 16 |
if src_dir not in sys.path:
|
| 17 |
sys.path.insert(0, src_dir)
|
| 18 |
|
| 19 |
+
# Configure Streamlit page
|
| 20 |
+
st.set_page_config(
|
| 21 |
+
page_title="π€ Agentic Browser",
|
| 22 |
+
page_icon="π€",
|
| 23 |
+
layout="wide",
|
| 24 |
+
initial_sidebar_state="expanded"
|
| 25 |
+
)
|
| 26 |
|
| 27 |
+
# Import the streamlit app components and run directly
|
| 28 |
+
try:
|
| 29 |
+
# Import all the UI components from streamlit_app
|
| 30 |
+
import streamlit_app
|
| 31 |
+
|
| 32 |
+
# The streamlit_app.py file contains the UI code that runs automatically
|
| 33 |
+
# when imported, so we don't need to call a function here
|
| 34 |
+
|
| 35 |
+
except ImportError as e:
|
| 36 |
+
st.error(f"Failed to import streamlit app: {e}")
|
| 37 |
+
st.info("Please check that all required files are present.")
|
| 38 |
+
except Exception as e:
|
| 39 |
+
st.error(f"An error occurred: {e}")
|
| 40 |
+
st.info("Please refresh the page or contact support.")
|
deploy.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Deployment script for Hugging Face Spaces
|
| 4 |
+
"""
|
| 5 |
+
import os
|
| 6 |
+
import subprocess
|
| 7 |
+
import sys
|
| 8 |
+
|
| 9 |
+
def run_command(command, description):
|
| 10 |
+
"""Run a shell command and handle errors."""
|
| 11 |
+
print(f"π§ {description}...")
|
| 12 |
+
try:
|
| 13 |
+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
|
| 14 |
+
if result.returncode == 0:
|
| 15 |
+
print(f"β
{description} completed successfully")
|
| 16 |
+
if result.stdout.strip():
|
| 17 |
+
print(f" Output: {result.stdout.strip()}")
|
| 18 |
+
else:
|
| 19 |
+
print(f"β {description} failed")
|
| 20 |
+
if result.stderr.strip():
|
| 21 |
+
print(f" Error: {result.stderr.strip()}")
|
| 22 |
+
return False
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print(f"β {description} failed with exception: {e}")
|
| 25 |
+
return False
|
| 26 |
+
return True
|
| 27 |
+
|
| 28 |
+
def main():
|
| 29 |
+
"""Main deployment function."""
|
| 30 |
+
print("π Agentic Browser - Hugging Face Spaces Deployment")
|
| 31 |
+
print("=" * 55)
|
| 32 |
+
|
| 33 |
+
# Check if we're in the right directory
|
| 34 |
+
if not os.path.exists("app.py"):
|
| 35 |
+
print("β Error: app.py not found. Please run this script from the project root.")
|
| 36 |
+
return False
|
| 37 |
+
|
| 38 |
+
# Check if git is installed
|
| 39 |
+
if not run_command("git --version", "Checking Git installation"):
|
| 40 |
+
print("β Git is not installed. Please install Git first.")
|
| 41 |
+
return False
|
| 42 |
+
|
| 43 |
+
# Initialize git if not already initialized
|
| 44 |
+
if not os.path.exists(".git"):
|
| 45 |
+
print("π Initializing Git repository...")
|
| 46 |
+
if not run_command("git init", "Git initialization"):
|
| 47 |
+
return False
|
| 48 |
+
|
| 49 |
+
# Check if there are any changes to commit
|
| 50 |
+
result = subprocess.run("git status --porcelain", shell=True, capture_output=True, text=True)
|
| 51 |
+
if not result.stdout.strip():
|
| 52 |
+
print("βΉοΈ No changes to commit.")
|
| 53 |
+
else:
|
| 54 |
+
print("π Changes detected:")
|
| 55 |
+
print(result.stdout)
|
| 56 |
+
|
| 57 |
+
# Add all files
|
| 58 |
+
if not run_command("git add .", "Adding files to Git"):
|
| 59 |
+
return False
|
| 60 |
+
|
| 61 |
+
# Commit changes
|
| 62 |
+
commit_message = input("Enter commit message (or press Enter for default): ").strip()
|
| 63 |
+
if not commit_message:
|
| 64 |
+
commit_message = "Update Agentic Browser for Hugging Face Spaces"
|
| 65 |
+
|
| 66 |
+
if not run_command(f'git commit -m "{commit_message}"', "Committing changes"):
|
| 67 |
+
return False
|
| 68 |
+
|
| 69 |
+
print("\nπ Repository is ready for deployment!")
|
| 70 |
+
print("\nπ Next steps:")
|
| 71 |
+
print("1. Create a new Space on Hugging Face: https://huggingface.co/spaces")
|
| 72 |
+
print("2. Choose:")
|
| 73 |
+
print(" - SDK: Streamlit")
|
| 74 |
+
print(" - Hardware: CPU basic (free) or upgrade as needed")
|
| 75 |
+
print("3. Clone your Space repository:")
|
| 76 |
+
print(" git clone https://huggingface.co/spaces/YOUR_USERNAME/SPACE_NAME")
|
| 77 |
+
print("4. Copy all files from this directory to your Space directory")
|
| 78 |
+
print("5. Push to Hugging Face:")
|
| 79 |
+
print(" cd SPACE_NAME")
|
| 80 |
+
print(" git add .")
|
| 81 |
+
print(" git commit -m 'Initial deployment'")
|
| 82 |
+
print(" git push origin main")
|
| 83 |
+
|
| 84 |
+
# Ask if user wants to see deployment checklist
|
| 85 |
+
show_checklist = input("\nWould you like to see the deployment checklist? (y/n): ").lower().startswith('y')
|
| 86 |
+
|
| 87 |
+
if show_checklist:
|
| 88 |
+
print("\nπ Deployment Checklist:")
|
| 89 |
+
print("β
All files are present and tested")
|
| 90 |
+
print("β
Requirements.txt is optimized for HF Spaces")
|
| 91 |
+
print("β
Import errors are fixed with fallbacks")
|
| 92 |
+
print("β
Streamlit app is properly configured")
|
| 93 |
+
print("β
Error handling is implemented")
|
| 94 |
+
print("β
User interface is responsive")
|
| 95 |
+
print("\nπ Files included:")
|
| 96 |
+
files = [
|
| 97 |
+
"app.py - Main entry point",
|
| 98 |
+
"requirements.txt - Python dependencies",
|
| 99 |
+
"packages.txt - System packages",
|
| 100 |
+
"README.md - Space description",
|
| 101 |
+
"src/streamlit_app.py - UI components",
|
| 102 |
+
"src/models/model_manager.py - Model handling",
|
| 103 |
+
"config/ - Configuration files"
|
| 104 |
+
]
|
| 105 |
+
for file in files:
|
| 106 |
+
print(f" π {file}")
|
| 107 |
+
|
| 108 |
+
return True
|
| 109 |
+
|
| 110 |
+
if __name__ == "__main__":
|
| 111 |
+
success = main()
|
| 112 |
+
if success:
|
| 113 |
+
print("\nπ Ready for Hugging Face Spaces deployment!")
|
| 114 |
+
else:
|
| 115 |
+
print("\nβ Deployment preparation failed. Please fix the issues above.")
|
| 116 |
+
sys.exit(0 if success else 1)
|
health_check.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Simple health check script for debugging deployment issues
|
| 3 |
+
"""
|
| 4 |
+
import sys
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
def main():
|
| 8 |
+
print("π₯ Health Check for Agentic Browser")
|
| 9 |
+
print("=" * 40)
|
| 10 |
+
|
| 11 |
+
# Check Python version
|
| 12 |
+
print(f"Python Version: {sys.version}")
|
| 13 |
+
|
| 14 |
+
# Check current directory
|
| 15 |
+
print(f"Current Directory: {os.getcwd()}")
|
| 16 |
+
|
| 17 |
+
# Check if key files exist
|
| 18 |
+
key_files = [
|
| 19 |
+
"app.py",
|
| 20 |
+
"requirements.txt",
|
| 21 |
+
"src/streamlit_app.py",
|
| 22 |
+
"src/models/model_manager.py"
|
| 23 |
+
]
|
| 24 |
+
|
| 25 |
+
print("\nπ File Check:")
|
| 26 |
+
for file in key_files:
|
| 27 |
+
exists = "β
" if os.path.exists(file) else "β"
|
| 28 |
+
print(f"{exists} {file}")
|
| 29 |
+
|
| 30 |
+
# Check imports
|
| 31 |
+
print("\nπ¦ Import Check:")
|
| 32 |
+
try:
|
| 33 |
+
import streamlit
|
| 34 |
+
print(f"β
Streamlit {streamlit.__version__}")
|
| 35 |
+
except ImportError as e:
|
| 36 |
+
print(f"β Streamlit: {e}")
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
import torch
|
| 40 |
+
print(f"β
PyTorch {torch.__version__}")
|
| 41 |
+
except ImportError as e:
|
| 42 |
+
print(f"β PyTorch: {e}")
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
import transformers
|
| 46 |
+
print(f"β
Transformers {transformers.__version__}")
|
| 47 |
+
except ImportError as e:
|
| 48 |
+
print(f"β Transformers: {e}")
|
| 49 |
+
|
| 50 |
+
# Test app import
|
| 51 |
+
print("\nπ§ App Import Test:")
|
| 52 |
+
try:
|
| 53 |
+
sys.path.insert(0, '.')
|
| 54 |
+
sys.path.insert(0, 'src')
|
| 55 |
+
import app
|
| 56 |
+
print("β
app.py imports successfully")
|
| 57 |
+
except Exception as e:
|
| 58 |
+
print(f"β app.py import failed: {e}")
|
| 59 |
+
|
| 60 |
+
print("\nβ
Health check complete!")
|
| 61 |
+
|
| 62 |
+
if __name__ == "__main__":
|
| 63 |
+
main()
|
src/streamlit_app.py
CHANGED
|
@@ -14,14 +14,13 @@ if parent_dir not in sys.path:
|
|
| 14 |
try:
|
| 15 |
from models.model_manager import model_manager
|
| 16 |
except ImportError:
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
st.set_page_config(
|
| 21 |
-
page_title="π€ Agentic Browser",
|
| 22 |
-
page_icon="π€",
|
| 23 |
-
layout="wide"
|
| 24 |
-
)
|
| 25 |
|
| 26 |
# Custom CSS for better styling
|
| 27 |
st.markdown("""
|
|
@@ -57,34 +56,68 @@ if 'messages' not in st.session_state:
|
|
| 57 |
def load_model(model_name):
|
| 58 |
"""Load the selected model."""
|
| 59 |
try:
|
| 60 |
-
with st.spinner(f"Loading {model_name}..."):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
model_manager.load_model(model_name)
|
|
|
|
|
|
|
|
|
|
| 62 |
st.session_state.model_loaded = True
|
| 63 |
st.session_state.current_model = model_name
|
| 64 |
-
st.success(f"Successfully loaded {model_name} model!")
|
| 65 |
return True
|
| 66 |
except Exception as e:
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
return False
|
| 69 |
|
| 70 |
def generate_response(prompt, model_name, temperature=0.7):
|
| 71 |
"""Generate a response using the selected model."""
|
| 72 |
try:
|
|
|
|
| 73 |
if not st.session_state.model_loaded or st.session_state.current_model != model_name:
|
|
|
|
| 74 |
if not load_model(model_name):
|
| 75 |
-
return "Error: Failed to load model."
|
| 76 |
|
| 77 |
-
#
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
return response
|
|
|
|
| 86 |
except Exception as e:
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
# Sidebar for settings
|
| 90 |
with st.sidebar:
|
|
@@ -109,35 +142,84 @@ with st.sidebar:
|
|
| 109 |
)
|
| 110 |
|
| 111 |
# Load model button
|
| 112 |
-
if st.button("π Load Model"):
|
| 113 |
load_model(selected_model)
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
st.markdown("---")
|
| 116 |
st.markdown("### About")
|
| 117 |
st.markdown("""
|
| 118 |
-
**Agentic Browser** is an AI-powered
|
| 119 |
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
""")
|
| 122 |
|
| 123 |
st.markdown("---")
|
| 124 |
st.markdown("### Models")
|
| 125 |
st.markdown("""
|
| 126 |
-
- **TinyLlama**: Fast
|
| 127 |
-
- **
|
|
|
|
|
|
|
| 128 |
""")
|
| 129 |
|
| 130 |
# Main chat interface
|
| 131 |
st.title("π€ Agentic Browser")
|
| 132 |
-
st.caption("Powered by
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
# Display chat messages
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
# Chat input
|
| 140 |
-
if prompt := st.chat_input("Type your message here..."):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
# Add user message to chat history
|
| 142 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 143 |
|
|
@@ -148,21 +230,32 @@ if prompt := st.chat_input("Type your message here..."):
|
|
| 148 |
# Generate and display assistant response
|
| 149 |
with st.chat_message("assistant"):
|
| 150 |
message_placeholder = st.empty()
|
| 151 |
-
full_response = ""
|
| 152 |
|
| 153 |
# Generate response
|
| 154 |
response = generate_response(prompt, selected_model, temperature)
|
| 155 |
|
| 156 |
-
#
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
# Auto-scroll to bottom
|
| 168 |
st.rerun()
|
|
|
|
| 14 |
try:
|
| 15 |
from models.model_manager import model_manager
|
| 16 |
except ImportError:
|
| 17 |
+
try:
|
| 18 |
+
from src.models.model_manager import model_manager
|
| 19 |
+
except ImportError:
|
| 20 |
+
st.error("β Failed to import model manager. Please check the installation.")
|
| 21 |
+
st.stop()
|
| 22 |
|
| 23 |
+
# Note: Page config is set in app.py to avoid conflicts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Custom CSS for better styling
|
| 26 |
st.markdown("""
|
|
|
|
| 56 |
def load_model(model_name):
|
| 57 |
"""Load the selected model."""
|
| 58 |
try:
|
| 59 |
+
with st.spinner(f"π Loading {model_name} model... This might take a few minutes on first load."):
|
| 60 |
+
# Show some progress info
|
| 61 |
+
progress_text = st.empty()
|
| 62 |
+
progress_text.text("Initializing model manager...")
|
| 63 |
+
|
| 64 |
model_manager.load_model(model_name)
|
| 65 |
+
|
| 66 |
+
progress_text.text("Model loaded successfully!")
|
| 67 |
+
|
| 68 |
st.session_state.model_loaded = True
|
| 69 |
st.session_state.current_model = model_name
|
| 70 |
+
st.success(f"β
Successfully loaded {model_name} model!")
|
| 71 |
return True
|
| 72 |
except Exception as e:
|
| 73 |
+
error_msg = str(e)
|
| 74 |
+
st.error(f"β Error loading model: {error_msg}")
|
| 75 |
+
|
| 76 |
+
# Provide helpful suggestions
|
| 77 |
+
if "OutOfMemoryError" in error_msg or "CUDA out of memory" in error_msg:
|
| 78 |
+
st.info("π‘ Try using the 'tiny-llama' model which requires less memory.")
|
| 79 |
+
elif "Connection" in error_msg or "timeout" in error_msg:
|
| 80 |
+
st.info("π‘ Check your internet connection. Model downloads require a stable connection.")
|
| 81 |
+
else:
|
| 82 |
+
st.info("π‘ Try refreshing the page or selecting a different model.")
|
| 83 |
+
|
| 84 |
return False
|
| 85 |
|
| 86 |
def generate_response(prompt, model_name, temperature=0.7):
|
| 87 |
"""Generate a response using the selected model."""
|
| 88 |
try:
|
| 89 |
+
# Check if model is loaded
|
| 90 |
if not st.session_state.model_loaded or st.session_state.current_model != model_name:
|
| 91 |
+
st.info(f"π Model not loaded. Loading {model_name}...")
|
| 92 |
if not load_model(model_name):
|
| 93 |
+
return "β Error: Failed to load model. Please try again or select a different model."
|
| 94 |
|
| 95 |
+
# Show generating status
|
| 96 |
+
with st.spinner("π€ Generating response..."):
|
| 97 |
+
# Generate response
|
| 98 |
+
response = model_manager.generate_text(
|
| 99 |
+
model_name=model_name,
|
| 100 |
+
prompt=prompt,
|
| 101 |
+
temperature=temperature,
|
| 102 |
+
max_length=1024
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
if not response or response.strip() == "":
|
| 106 |
+
return "β οΈ The model generated an empty response. Please try rephrasing your question."
|
| 107 |
|
| 108 |
return response
|
| 109 |
+
|
| 110 |
except Exception as e:
|
| 111 |
+
error_msg = str(e)
|
| 112 |
+
st.error(f"β Error generating response: {error_msg}")
|
| 113 |
+
|
| 114 |
+
# Provide helpful suggestions based on error type
|
| 115 |
+
if "OutOfMemoryError" in error_msg:
|
| 116 |
+
return "π Out of memory error. Try using a shorter prompt or the 'tiny-llama' model."
|
| 117 |
+
elif "timeout" in error_msg.lower():
|
| 118 |
+
return "β° Request timed out. Please try again with a shorter prompt."
|
| 119 |
+
else:
|
| 120 |
+
return f"β An error occurred while generating the response. Please try again.\n\nTechnical details: {error_msg}"
|
| 121 |
|
| 122 |
# Sidebar for settings
|
| 123 |
with st.sidebar:
|
|
|
|
| 142 |
)
|
| 143 |
|
| 144 |
# Load model button
|
| 145 |
+
if st.button("π Load Model", type="primary"):
|
| 146 |
load_model(selected_model)
|
| 147 |
|
| 148 |
+
# Clear chat button
|
| 149 |
+
if st.button("ποΈ Clear Chat"):
|
| 150 |
+
st.session_state.messages = []
|
| 151 |
+
st.rerun()
|
| 152 |
+
|
| 153 |
+
# Model status
|
| 154 |
+
if st.session_state.model_loaded:
|
| 155 |
+
st.success(f"β
Current: {st.session_state.current_model}")
|
| 156 |
+
else:
|
| 157 |
+
st.warning("β οΈ No model loaded")
|
| 158 |
+
|
| 159 |
st.markdown("---")
|
| 160 |
st.markdown("### About")
|
| 161 |
st.markdown("""
|
| 162 |
+
**Agentic Browser** is an AI-powered chat assistant using open-source language models.
|
| 163 |
|
| 164 |
+
π **Features:**
|
| 165 |
+
- Multiple model options
|
| 166 |
+
- Real-time chat interface
|
| 167 |
+
- Adjustable creativity settings
|
| 168 |
+
- No data collection
|
| 169 |
""")
|
| 170 |
|
| 171 |
st.markdown("---")
|
| 172 |
st.markdown("### Models")
|
| 173 |
st.markdown("""
|
| 174 |
+
- **TinyLlama**: Fast and lightweight (1.1B parameters)
|
| 175 |
+
- **DialoGPT**: Conversational AI optimized for chat
|
| 176 |
+
|
| 177 |
+
π‘ **Tip:** Start with TinyLlama for faster responses!
|
| 178 |
""")
|
| 179 |
|
| 180 |
# Main chat interface
|
| 181 |
st.title("π€ Agentic Browser")
|
| 182 |
+
st.caption("Powered by open-source AI models")
|
| 183 |
+
|
| 184 |
+
# Show status
|
| 185 |
+
if st.session_state.model_loaded:
|
| 186 |
+
st.success(f"β
Model loaded: {st.session_state.current_model}")
|
| 187 |
+
else:
|
| 188 |
+
st.info("βΉοΈ Please select and load a model from the sidebar to start chatting.")
|
| 189 |
|
| 190 |
# Display chat messages
|
| 191 |
+
if st.session_state.messages:
|
| 192 |
+
for message in st.session_state.messages:
|
| 193 |
+
with st.chat_message(message["role"]):
|
| 194 |
+
st.markdown(message["content"])
|
| 195 |
+
else:
|
| 196 |
+
# Show welcome message
|
| 197 |
+
st.markdown("""
|
| 198 |
+
### π Welcome to Agentic Browser!
|
| 199 |
+
|
| 200 |
+
This is an AI-powered chat interface using open-source language models.
|
| 201 |
+
|
| 202 |
+
**Getting Started:**
|
| 203 |
+
1. π Select a model from the sidebar
|
| 204 |
+
2. Click "π Load Model" to initialize it
|
| 205 |
+
3. Start typing your message below
|
| 206 |
+
|
| 207 |
+
**Available Models:**
|
| 208 |
+
- **TinyLlama**: Fast and efficient, great for quick responses
|
| 209 |
+
- **DialoGPT**: Optimized for conversations
|
| 210 |
+
|
| 211 |
+
Try asking questions like:
|
| 212 |
+
- "Explain quantum computing in simple terms"
|
| 213 |
+
- "Write a short story about a robot"
|
| 214 |
+
- "Help me plan a vacation"
|
| 215 |
+
""")
|
| 216 |
|
| 217 |
# Chat input
|
| 218 |
+
if prompt := st.chat_input("Type your message here...", disabled=not st.session_state.model_loaded):
|
| 219 |
+
if not st.session_state.model_loaded:
|
| 220 |
+
st.warning("β οΈ Please load a model first before chatting.")
|
| 221 |
+
st.stop()
|
| 222 |
+
|
| 223 |
# Add user message to chat history
|
| 224 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 225 |
|
|
|
|
| 230 |
# Generate and display assistant response
|
| 231 |
with st.chat_message("assistant"):
|
| 232 |
message_placeholder = st.empty()
|
|
|
|
| 233 |
|
| 234 |
# Generate response
|
| 235 |
response = generate_response(prompt, selected_model, temperature)
|
| 236 |
|
| 237 |
+
# Check if response is valid
|
| 238 |
+
if response and not response.startswith("β") and not response.startswith("π"):
|
| 239 |
+
# Display response with streaming effect
|
| 240 |
+
full_response = ""
|
| 241 |
+
words = response.split()
|
| 242 |
+
|
| 243 |
+
for i, word in enumerate(words):
|
| 244 |
+
full_response += word + " "
|
| 245 |
+
time.sleep(0.05) # Adjust speed as needed
|
| 246 |
+
# Show cursor while typing
|
| 247 |
+
cursor = "β" if i < len(words) - 1 else ""
|
| 248 |
+
message_placeholder.markdown(full_response + cursor)
|
| 249 |
+
|
| 250 |
+
# Final message without cursor
|
| 251 |
+
message_placeholder.markdown(full_response.strip())
|
| 252 |
+
|
| 253 |
+
# Add assistant response to chat history
|
| 254 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response.strip()})
|
| 255 |
+
else:
|
| 256 |
+
# Display error message directly
|
| 257 |
+
message_placeholder.markdown(response)
|
| 258 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 259 |
|
| 260 |
# Auto-scroll to bottom
|
| 261 |
st.rerun()
|