Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,22 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from langchain_community.llms import HuggingFaceHub #
|
| 3 |
from langchain.chains import ConversationChain
|
| 4 |
from langchain.memory import ConversationBufferMemory
|
| 5 |
import os
|
| 6 |
from datetime import datetime, timedelta
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# Load environment variables - MUST be at the top
|
| 10 |
load_dotenv('.env') # Explicitly load from .env file
|
| 11 |
|
| 12 |
-
# Debugging - check if token is available (remove in production)
|
| 13 |
-
token = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
| 14 |
-
if token:
|
| 15 |
-
print("Token found! Length:", len(token))
|
| 16 |
-
else:
|
| 17 |
-
print("WARNING: Token not found in environment variables!")
|
| 18 |
-
|
| 19 |
# Configure page
|
| 20 |
st.set_page_config(
|
| 21 |
page_title="Tourism Chatbot",
|
|
@@ -50,21 +51,15 @@ language = st.selectbox(
|
|
| 50 |
key="lang_select"
|
| 51 |
)
|
| 52 |
|
| 53 |
-
# Get token
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
#
|
| 57 |
-
if not
|
| 58 |
-
st.
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
3. On Hugging Face Spaces, add it as a secret
|
| 63 |
-
""")
|
| 64 |
-
|
| 65 |
-
# Allow token input directly in the app (for development only)
|
| 66 |
-
hf_token = st.text_input("Enter your Hugging Face token:", type="password")
|
| 67 |
-
if not hf_token:
|
| 68 |
st.stop()
|
| 69 |
|
| 70 |
# Enhanced model configuration
|
|
@@ -73,28 +68,23 @@ model_config = {
|
|
| 73 |
"repo_id": "google/flan-t5-base", # Using a smaller model for testing
|
| 74 |
"params": {
|
| 75 |
"temperature": 0.7,
|
| 76 |
-
"max_length": 512
|
| 77 |
-
"max_new_tokens": 300,
|
| 78 |
-
"repetition_penalty": 1.2
|
| 79 |
}
|
| 80 |
},
|
| 81 |
"العربية": {
|
| 82 |
"repo_id": "aubmindlab/aragpt2-medium", # Using a smaller model for testing
|
| 83 |
"params": {
|
| 84 |
"temperature": 0.6,
|
| 85 |
-
"max_length": 1024
|
| 86 |
-
"max_new_tokens": 400,
|
| 87 |
-
"repetition_penalty": 1.3
|
| 88 |
}
|
| 89 |
}
|
| 90 |
}
|
| 91 |
|
| 92 |
# Initialize the language model with enhanced error handling
|
| 93 |
try:
|
| 94 |
-
#
|
| 95 |
llm = HuggingFaceHub(
|
| 96 |
repo_id=model_config[language]["repo_id"],
|
| 97 |
-
huggingfacehub_api_token=hf_token, # CORRECT PARAMETER NAME
|
| 98 |
model_kwargs=model_config[language]["params"]
|
| 99 |
)
|
| 100 |
|
|
@@ -103,13 +93,20 @@ try:
|
|
| 103 |
memory=st.session_state.memory,
|
| 104 |
verbose=False
|
| 105 |
)
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
except Exception as e:
|
| 111 |
error_msg = str(e)
|
| 112 |
-
st.error(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
st.stop()
|
| 114 |
|
| 115 |
# Display chat history with improved formatting
|
|
@@ -214,4 +211,11 @@ with st.sidebar:
|
|
| 214 |
if st.button("Reset Conversation" if language == "English" else "إعادة ضبط المحادثة"):
|
| 215 |
st.session_state.messages = []
|
| 216 |
st.session_state.memory.clear()
|
| 217 |
-
st.experimental_rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from langchain_community.llms import HuggingFaceHub # Updated import
|
| 3 |
from langchain.chains import ConversationChain
|
| 4 |
from langchain.memory import ConversationBufferMemory
|
| 5 |
import os
|
| 6 |
from datetime import datetime, timedelta
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
|
| 9 |
+
# Print version information for debugging
|
| 10 |
+
try:
|
| 11 |
+
import langchain_community
|
| 12 |
+
|
| 13 |
+
print(f"langchain_community version: {langchain_community.__version__}")
|
| 14 |
+
except Exception as e:
|
| 15 |
+
print(f"Error getting version info: {e}")
|
| 16 |
+
|
| 17 |
# Load environment variables - MUST be at the top
|
| 18 |
load_dotenv('.env') # Explicitly load from .env file
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
# Configure page
|
| 21 |
st.set_page_config(
|
| 22 |
page_title="Tourism Chatbot",
|
|
|
|
| 51 |
key="lang_select"
|
| 52 |
)
|
| 53 |
|
| 54 |
+
# Get token from environment
|
| 55 |
+
token = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
| 56 |
+
|
| 57 |
+
# For development, allow manual token entry if not found in environment
|
| 58 |
+
if not token:
|
| 59 |
+
st.warning("API token not found in environment variables.")
|
| 60 |
+
token = st.text_input("Enter your Hugging Face API token:", type="password")
|
| 61 |
+
if not token:
|
| 62 |
+
st.error("Token is required to proceed.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
st.stop()
|
| 64 |
|
| 65 |
# Enhanced model configuration
|
|
|
|
| 68 |
"repo_id": "google/flan-t5-base", # Using a smaller model for testing
|
| 69 |
"params": {
|
| 70 |
"temperature": 0.7,
|
| 71 |
+
"max_length": 512
|
|
|
|
|
|
|
| 72 |
}
|
| 73 |
},
|
| 74 |
"العربية": {
|
| 75 |
"repo_id": "aubmindlab/aragpt2-medium", # Using a smaller model for testing
|
| 76 |
"params": {
|
| 77 |
"temperature": 0.6,
|
| 78 |
+
"max_length": 1024
|
|
|
|
|
|
|
| 79 |
}
|
| 80 |
}
|
| 81 |
}
|
| 82 |
|
| 83 |
# Initialize the language model with enhanced error handling
|
| 84 |
try:
|
| 85 |
+
# Simple initialization - let the library handle the token from env vars
|
| 86 |
llm = HuggingFaceHub(
|
| 87 |
repo_id=model_config[language]["repo_id"],
|
|
|
|
| 88 |
model_kwargs=model_config[language]["params"]
|
| 89 |
)
|
| 90 |
|
|
|
|
| 93 |
memory=st.session_state.memory,
|
| 94 |
verbose=False
|
| 95 |
)
|
| 96 |
+
|
| 97 |
+
st.success("Model connected successfully!")
|
| 98 |
+
|
|
|
|
| 99 |
except Exception as e:
|
| 100 |
error_msg = str(e)
|
| 101 |
+
st.error(f"Error initializing model: {error_msg}")
|
| 102 |
+
|
| 103 |
+
if "token" in error_msg.lower() or "api" in error_msg.lower():
|
| 104 |
+
st.info("""
|
| 105 |
+
Make sure the token is set correctly:
|
| 106 |
+
1. Create a Hugging Face account and get a token
|
| 107 |
+
2. Set it as an environment variable: `setx HUGGINGFACEHUB_API_TOKEN "your_token"`
|
| 108 |
+
3. Restart your terminal after setting the environment variable
|
| 109 |
+
""")
|
| 110 |
st.stop()
|
| 111 |
|
| 112 |
# Display chat history with improved formatting
|
|
|
|
| 211 |
if st.button("Reset Conversation" if language == "English" else "إعادة ضبط المحادثة"):
|
| 212 |
st.session_state.messages = []
|
| 213 |
st.session_state.memory.clear()
|
| 214 |
+
st.experimental_rerun()
|
| 215 |
+
|
| 216 |
+
# Add a manual token entry option in sidebar for easy updating
|
| 217 |
+
st.subheader("API Configuration")
|
| 218 |
+
new_token = st.text_input("Update API Token:", type="password")
|
| 219 |
+
if st.button("Save Token"):
|
| 220 |
+
os.environ["HUGGINGFACEHUB_API_TOKEN"] = new_token
|
| 221 |
+
st.success("Token updated! Restart the app to apply.")
|