Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
import streamlit as st
|
2 |
-
from
|
3 |
from langchain.chains import ConversationChain
|
4 |
from langchain.memory import ConversationBufferMemory
|
5 |
import os
|
@@ -9,6 +9,13 @@ from dotenv import load_dotenv
|
|
9 |
# Load environment variables - MUST be at the top
|
10 |
load_dotenv('.env') # Explicitly load from .env file
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
# Configure page
|
13 |
st.set_page_config(
|
14 |
page_title="Tourism Chatbot",
|
@@ -43,20 +50,27 @@ language = st.selectbox(
|
|
43 |
key="lang_select"
|
44 |
)
|
45 |
|
46 |
-
#
|
47 |
-
|
48 |
-
|
|
|
|
|
49 |
st.error("""
|
50 |
-
API token not
|
51 |
1. Create a .env file with HUGGINGFACEHUB_API_TOKEN=your_token_here
|
52 |
-
2.
|
|
|
53 |
""")
|
54 |
-
|
|
|
|
|
|
|
|
|
55 |
|
56 |
# Enhanced model configuration
|
57 |
model_config = {
|
58 |
"English": {
|
59 |
-
"repo_id": "google/flan-t5-
|
60 |
"params": {
|
61 |
"temperature": 0.7,
|
62 |
"max_length": 512,
|
@@ -65,7 +79,7 @@ model_config = {
|
|
65 |
}
|
66 |
},
|
67 |
"العربية": {
|
68 |
-
"repo_id": "aubmindlab/aragpt2-
|
69 |
"params": {
|
70 |
"temperature": 0.6,
|
71 |
"max_length": 1024,
|
@@ -77,10 +91,10 @@ model_config = {
|
|
77 |
|
78 |
# Initialize the language model with enhanced error handling
|
79 |
try:
|
80 |
-
# This is the
|
81 |
llm = HuggingFaceHub(
|
82 |
repo_id=model_config[language]["repo_id"],
|
83 |
-
huggingfacehub_api_token=
|
84 |
model_kwargs=model_config[language]["params"]
|
85 |
)
|
86 |
|
@@ -89,14 +103,13 @@ try:
|
|
89 |
memory=st.session_state.memory,
|
90 |
verbose=False
|
91 |
)
|
|
|
|
|
|
|
|
|
92 |
except Exception as e:
|
93 |
error_msg = str(e)
|
94 |
-
|
95 |
-
st.error("Invalid API token. Please check your Hugging Face token.")
|
96 |
-
elif "repo_id" in error_msg:
|
97 |
-
st.error("Model loading failed. The specified model may not be available.")
|
98 |
-
else:
|
99 |
-
st.error(f"Initialization error: {error_msg}")
|
100 |
st.stop()
|
101 |
|
102 |
# Display chat history with improved formatting
|
|
|
1 |
import streamlit as st
|
2 |
+
from langchain_community.llms import HuggingFaceHub # Use updated import
|
3 |
from langchain.chains import ConversationChain
|
4 |
from langchain.memory import ConversationBufferMemory
|
5 |
import os
|
|
|
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 |
key="lang_select"
|
51 |
)
|
52 |
|
53 |
+
# Get token - try both environment variable and direct input
|
54 |
+
hf_token = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
55 |
+
|
56 |
+
# Display token status
|
57 |
+
if not hf_token:
|
58 |
st.error("""
|
59 |
+
API token not found in environment variables. Please:
|
60 |
1. Create a .env file with HUGGINGFACEHUB_API_TOKEN=your_token_here
|
61 |
+
2. Set the environment variable using setx on Windows
|
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
|
71 |
model_config = {
|
72 |
"English": {
|
73 |
+
"repo_id": "google/flan-t5-base", # Using a smaller model for testing
|
74 |
"params": {
|
75 |
"temperature": 0.7,
|
76 |
"max_length": 512,
|
|
|
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,
|
|
|
91 |
|
92 |
# Initialize the language model with enhanced error handling
|
93 |
try:
|
94 |
+
# This is the key fix - correct parameter name is huggingfacehub_api_token
|
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 |
memory=st.session_state.memory,
|
104 |
verbose=False
|
105 |
)
|
106 |
+
|
107 |
+
# Show success message
|
108 |
+
st.success("Successfully connected to Hugging Face model!")
|
109 |
+
|
110 |
except Exception as e:
|
111 |
error_msg = str(e)
|
112 |
+
st.error(f"Initialization error: {error_msg}")
|
|
|
|
|
|
|
|
|
|
|
113 |
st.stop()
|
114 |
|
115 |
# Display chat history with improved formatting
|