moosalah commited on
Commit
13a0fdd
·
verified ·
1 Parent(s): 5762c64

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from langchain.llms import HuggingFaceHub
3
+ from langchain.chains import ConversationChain
4
+ from langchain.memory import ConversationBufferMemory
5
+ import os
6
+
7
+ # Configure page
8
+ st.set_page_config(page_title="Tourism Chatbot", page_icon="🌍")
9
+
10
+ # Title
11
+ st.title("Tourism Assistant - مساعد السياحة")
12
+
13
+ # Initialize session state for memory
14
+ if "memory" not in st.session_state:
15
+ st.session_state.memory = ConversationBufferMemory(return_messages=True)
16
+
17
+ # Initialize session state for chat history
18
+ if "messages" not in st.session_state:
19
+ st.session_state.messages = []
20
+
21
+ # Language selector
22
+ language = st.selectbox("Choose Language / اختر اللغة", ["English", "العربية"])
23
+
24
+ # Hugging Face API token
25
+ hf_api_token = os.environ.get("HF_API_TOKEN", "")
26
+ if not hf_api_token:
27
+ st.error("Hugging Face API token not found. Please set it in the Space secrets.")
28
+ st.stop()
29
+
30
+ # Select the appropriate model based on language
31
+ model_name = "bigscience/bloom" if language == "English" else "bigscience/bloom"
32
+
33
+ # Initialize the language model
34
+ llm = HuggingFaceHub(
35
+ repo_id=model_name,
36
+ huggingface_api_token=hf_api_token,
37
+ model_kwargs={"temperature": 0.7, "max_length": 512}
38
+ )
39
+
40
+ # Initialize conversation chain
41
+ conversation = ConversationChain(
42
+ llm=llm,
43
+ memory=st.session_state.memory,
44
+ verbose=True
45
+ )
46
+
47
+ # Display chat history
48
+ for message in st.session_state.messages:
49
+ with st.chat_message(message["role"]):
50
+ st.markdown(message["content"])
51
+
52
+ # User input
53
+ if language == "English":
54
+ prompt = st.chat_input("Ask about tourist destinations, recommendations, or travel tips...")
55
+ else:
56
+ prompt = st.chat_input("اسأل عن الوجهات السياحية أو التوصيات أو نصائح السفر...")
57
+
58
+ # Process the user input
59
+ if prompt:
60
+ # Add user message to chat history
61
+ st.session_state.messages.append({"role": "user", "content": prompt})
62
+
63
+ # Display user message
64
+ with st.chat_message("user"):
65
+ st.markdown(prompt)
66
+
67
+ # Display assistant response with a spinner
68
+ with st.chat_message("assistant"):
69
+ with st.spinner("Thinking..."):
70
+ # Add context for tourism assistant
71
+ if language == "English":
72
+ full_prompt = f"You are a helpful tourism assistant. Answer the following question about travel and tourism: {prompt}"
73
+ else:
74
+ full_prompt = f"أنت مساعد سياحي مفيد. أجب عن السؤال التالي حول السفر والسياحة: {prompt}"
75
+
76
+ response = conversation.predict(input=full_prompt)
77
+ st.markdown(response)
78
+
79
+ # Add assistant response to chat history
80
+ st.session_state.messages.append({"role": "assistant", "content": response})
81
+
82
+ # Sidebar with information
83
+ with st.sidebar:
84
+ st.header("About this Chatbot")
85
+ if language == "English":
86
+ st.write(
87
+ "This is a tourism assistant that can help you with travel recommendations, destination information, and travel tips.")
88
+ st.write("Feel free to ask questions in English or Arabic!")
89
+ else:
90
+ st.write("هذا مساعد سياحي يمكنه مساعدتك في توصيات السفر ومعلومات الوجهة ونصائح السفر.")
91
+ st.write("لا تتردد في طرح الأسئلة باللغة الإنجليزية أو العربية!")