|
import streamlit as st |
|
from langchain.llms import HuggingFaceHub |
|
from langchain.chains import ConversationChain |
|
from langchain.memory import ConversationBufferMemory |
|
import os |
|
|
|
|
|
st.set_page_config(page_title="Tourism Chatbot", page_icon="🌍") |
|
|
|
|
|
st.title("Tourism Assistant - مساعد السياحة") |
|
|
|
|
|
if "memory" not in st.session_state: |
|
st.session_state.memory = ConversationBufferMemory(return_messages=True) |
|
|
|
|
|
if "messages" not in st.session_state: |
|
st.session_state.messages = [] |
|
|
|
|
|
language = st.selectbox("Choose Language / اختر اللغة", ["English", "العربية"]) |
|
|
|
|
|
hf_api_token = os.environ.get("HF_API_TOKEN", "") |
|
if not hf_api_token: |
|
st.error("Hugging Face API token not found. Please set it in the Space secrets.") |
|
st.stop() |
|
|
|
|
|
model_name = "bigscience/bloom" if language == "English" else "bigscience/bloom" |
|
|
|
|
|
llm = HuggingFaceHub( |
|
repo_id=model_name, |
|
huggingface_api_token=hf_api_token, |
|
model_kwargs={"temperature": 0.7, "max_length": 512} |
|
) |
|
|
|
|
|
conversation = ConversationChain( |
|
llm=llm, |
|
memory=st.session_state.memory, |
|
verbose=True |
|
) |
|
|
|
|
|
for message in st.session_state.messages: |
|
with st.chat_message(message["role"]): |
|
st.markdown(message["content"]) |
|
|
|
|
|
if language == "English": |
|
prompt = st.chat_input("Ask about tourist destinations, recommendations, or travel tips...") |
|
else: |
|
prompt = st.chat_input("اسأل عن الوجهات السياحية أو التوصيات أو نصائح السفر...") |
|
|
|
|
|
if prompt: |
|
|
|
st.session_state.messages.append({"role": "user", "content": prompt}) |
|
|
|
|
|
with st.chat_message("user"): |
|
st.markdown(prompt) |
|
|
|
|
|
with st.chat_message("assistant"): |
|
with st.spinner("Thinking..."): |
|
|
|
if language == "English": |
|
full_prompt = f"You are a helpful tourism assistant. Answer the following question about travel and tourism: {prompt}" |
|
else: |
|
full_prompt = f"أنت مساعد سياحي مفيد. أجب عن السؤال التالي حول السفر والسياحة: {prompt}" |
|
|
|
response = conversation.predict(input=full_prompt) |
|
st.markdown(response) |
|
|
|
|
|
st.session_state.messages.append({"role": "assistant", "content": response}) |
|
|
|
|
|
with st.sidebar: |
|
st.header("About this Chatbot") |
|
if language == "English": |
|
st.write( |
|
"This is a tourism assistant that can help you with travel recommendations, destination information, and travel tips.") |
|
st.write("Feel free to ask questions in English or Arabic!") |
|
else: |
|
st.write("هذا مساعد سياحي يمكنه مساعدتك في توصيات السفر ومعلومات الوجهة ونصائح السفر.") |
|
st.write("لا تتردد في طرح الأسئلة باللغة الإنجليزية أو العربية!") |