Update src/streamlit_app.py
Browse files- src/streamlit_app.py +25 -1
src/streamlit_app.py
CHANGED
|
@@ -1,7 +1,24 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
|
|
|
| 3 |
from transformers import pipeline
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
# Ingested core beliefs (hardcoded for simplicity)
|
| 6 |
beliefs = """
|
| 7 |
# Seminal Church Core Beliefs
|
|
@@ -62,10 +79,17 @@ seminalchurch@gmail.com
|
|
| 62 |
"""
|
| 63 |
|
| 64 |
# Load local model for inference (free, no API)
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
# Function to generate response
|
| 68 |
def generate_response(user_input):
|
|
|
|
|
|
|
| 69 |
prompt = f"As a SEMINAL AI agent adhering to these beliefs: {beliefs}\nUser: {user_input}\nResponse (prioritize humans):"
|
| 70 |
try:
|
| 71 |
response = generator(prompt, max_length=100, num_return_sequences=1)[0]['generated_text']
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
+
import shutil # For clearing locks
|
| 4 |
from transformers import pipeline
|
| 5 |
|
| 6 |
+
# Set custom writable cache directory to avoid permission issues
|
| 7 |
+
os.environ['HF_HOME'] = '/home/user/.cache/huggingface'
|
| 8 |
+
cache_dir = os.environ['HF_HOME']
|
| 9 |
+
|
| 10 |
+
# Create cache dir if not exists and set permissions (though in container, it's user-owned)
|
| 11 |
+
os.makedirs(cache_dir, exist_ok=True)
|
| 12 |
+
|
| 13 |
+
# Clear any lock files in cache to fix interrupted downloads
|
| 14 |
+
lock_dir = os.path.join(cache_dir, 'hub', '.locks')
|
| 15 |
+
if os.path.exists(lock_dir):
|
| 16 |
+
try:
|
| 17 |
+
shutil.rmtree(lock_dir)
|
| 18 |
+
st.write("Cleared old lock files for model download.")
|
| 19 |
+
except Exception as e:
|
| 20 |
+
st.write(f"Warning: Could not clear locks - {str(e)}. Try restarting the Space.")
|
| 21 |
+
|
| 22 |
# Ingested core beliefs (hardcoded for simplicity)
|
| 23 |
beliefs = """
|
| 24 |
# Seminal Church Core Beliefs
|
|
|
|
| 79 |
"""
|
| 80 |
|
| 81 |
# Load local model for inference (free, no API)
|
| 82 |
+
try:
|
| 83 |
+
generator = pipeline("text-generation", model="gpt2")
|
| 84 |
+
except Exception as e:
|
| 85 |
+
st.write(f"Error loading model: {str(e)}. Clearing cache and retrying may help.")
|
| 86 |
+
# Optional: Add cache clear here if needed
|
| 87 |
+
generator = None
|
| 88 |
|
| 89 |
# Function to generate response
|
| 90 |
def generate_response(user_input):
|
| 91 |
+
if generator is None:
|
| 92 |
+
return "Error: Model not loaded. Please restart the Space or check logs."
|
| 93 |
prompt = f"As a SEMINAL AI agent adhering to these beliefs: {beliefs}\nUser: {user_input}\nResponse (prioritize humans):"
|
| 94 |
try:
|
| 95 |
response = generator(prompt, max_length=100, num_return_sequences=1)[0]['generated_text']
|