dineth554 commited on
Commit
6f48855
·
verified ·
1 Parent(s): bf01695

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -49
app.py CHANGED
@@ -1,9 +1,12 @@
1
  import os
 
 
2
 
3
  # Install necessary libraries using os.system
4
  os.system("pip install --upgrade pip")
5
  os.system("pip install streamlit llama-cpp-agent huggingface_hub trafilatura beautifulsoup4 requests duckduckgo-search googlesearch-python")
6
 
 
7
  try:
8
  from llama_cpp import Llama
9
  from llama_cpp_agent.providers import LlamaCppPythonProvider
@@ -19,10 +22,21 @@ try:
19
  from utils import CitingSources
20
  from settings import get_context_by_model, get_messages_formatter_type
21
  except ImportError as e:
22
- raise ImportError(f"Error importing modules: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  import logging
25
- import streamlit as st
26
  from huggingface_hub import hf_hub_download
27
 
28
  # Download the models
@@ -43,7 +57,9 @@ hf_hub_download(
43
  )
44
 
45
  # Function to respond to user messages
46
- def respond(message, history, model, system_message, max_tokens, temperature, top_p, top_k, repeat_penalty):
 
 
47
  chat_template = get_messages_formatter_type(model)
48
  llm = Llama(
49
  model_path=f"models/{model}",
@@ -80,7 +96,6 @@ def respond(message, history, model, system_message, max_tokens, temperature, to
80
  settings.temperature = temperature
81
  settings.top_k = top_k
82
  settings.top_p = top_p
83
-
84
  settings.max_tokens = max_tokens
85
  settings.repeat_penalty = repeat_penalty
86
 
@@ -139,51 +154,19 @@ def respond(message, history, model, system_message, max_tokens, temperature, to
139
  outputs += "\n".join(citing_sources.sources)
140
  yield outputs
141
 
142
- # Streamlit app
143
- st.title("Llama-CPP-Agent Chatbot with Web Search")
144
-
145
- # Sidebar for settings
146
- st.sidebar.title("Settings")
147
- model = st.sidebar.selectbox(
148
- "Model",
149
- [
150
- 'Mistral-7B-Instruct-v0.3-Q6_K.gguf',
151
- 'mixtral-8x7b-instruct-v0.1.Q5_K_M.gguf',
152
- 'Meta-Llama-3-8B-Instruct-Q6_K.gguf'
153
- ]
154
- )
155
- system_message = st.sidebar.text_area("System message", value=web_search_system_prompt)
156
- max_tokens = st.sidebar.slider("Max tokens", min_value=1, max_value=4096, value=2048, step=1)
157
- temperature = st.sidebar.slider("Temperature", min_value=0.1, max_value=1.0, value=0.45, step=0.1)
158
- top_p = st.sidebar.slider("Top-p", min_value=0.1, max_value=1.0, value=0.95, step=0.05)
159
- top_k = st.sidebar.slider("Top-k", min_value=0, max_value=100, value=40, step=1)
160
- repeat_penalty = st.sidebar.slider("Repetition penalty", min_value=0.0, max_value=2.0, value=1.1, step=0.1)
161
-
162
- # Chat history
163
- if "history" not in st.session_state:
164
- st.session_state.history = []
165
 
166
- # Chat input
167
- message = st.text_input("You:", key="input")
 
 
 
 
 
168
 
169
  if st.button("Send"):
170
- history = st.session_state.history
171
- response = respond(
172
- message,
173
- history,
174
- model,
175
- system_message,
176
- max_tokens,
177
- temperature,
178
- top_p,
179
- top_k,
180
- repeat_penalty
181
- )
182
-
183
- for res in response:
184
- st.session_state.history.append((message, res))
185
- st.text_area("Chat", value=f"You: {message}\nBot: {res}", height=300)
186
-
187
- # Display chat history
188
- for user_msg, bot_msg in st.session_state.history:
189
- st.text_area("Chat", value=f"You: {user_msg}\nBot: {bot_msg}", height=300)
 
1
  import os
2
+ import logging
3
+ import streamlit as st
4
 
5
  # Install necessary libraries using os.system
6
  os.system("pip install --upgrade pip")
7
  os.system("pip install streamlit llama-cpp-agent huggingface_hub trafilatura beautifulsoup4 requests duckduckgo-search googlesearch-python")
8
 
9
+ # Attempt to import all required modules
10
  try:
11
  from llama_cpp import Llama
12
  from llama_cpp_agent.providers import LlamaCppPythonProvider
 
22
  from utils import CitingSources
23
  from settings import get_context_by_model, get_messages_formatter_type
24
  except ImportError as e:
25
+ st.error(f"Error importing modules: {e}")
26
+ if 'utils' in str(e):
27
+ st.warning("Mocking utils.CitingSources")
28
+ class CitingSources:
29
+ sources = []
30
+
31
+ if 'settings' in str(e):
32
+ st.warning("Mocking settings functions")
33
+ def get_context_by_model(model):
34
+ return 4096
35
+
36
+ def get_messages_formatter_type(model):
37
+ return MessagesFormatterType.BASIC
38
 
39
  import logging
 
40
  from huggingface_hub import hf_hub_download
41
 
42
  # Download the models
 
57
  )
58
 
59
  # Function to respond to user messages
60
+ def respond(message, history, system_message, temperature, top_p, top_k, repeat_penalty):
61
+ model = "mixtral-8x7b-instruct-v0.1.Q5_K_M.gguf"
62
+ max_tokens = 3000
63
  chat_template = get_messages_formatter_type(model)
64
  llm = Llama(
65
  model_path=f"models/{model}",
 
96
  settings.temperature = temperature
97
  settings.top_k = top_k
98
  settings.top_p = top_p
 
99
  settings.max_tokens = max_tokens
100
  settings.repeat_penalty = repeat_penalty
101
 
 
154
  outputs += "\n".join(citing_sources.sources)
155
  yield outputs
156
 
157
+ st.title("Novav2 Web Engine")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
 
159
+ message = st.text_input("Enter your message:")
160
+ history = st.session_state.get("history", [])
161
+ system_message = st.text_area("System message", value=web_search_system_prompt)
162
+ temperature = st.slider("Temperature", min_value=0.1, max_value=1.0, value=0.45, step=0.1)
163
+ top_p = st.slider("Top-p", min_value=0.1, max_value=1.0, value=0.95, step=0.05)
164
+ top_k = st.slider("Top-k", min_value=0, max_value=100, value=40, step=1)
165
+ repeat_penalty = st.slider("Repetition penalty", min_value=0.0, max_value=2.0, value=1.1, step=0.1)
166
 
167
  if st.button("Send"):
168
+ response_generator = respond(message, history, system_message, temperature, top_p, top_k, repeat_penalty)
169
+ for response in response_generator:
170
+ st.write(response)
171
+ history.append((message, response))
172
+ st.session_state["history"] = history