halimbahae commited on
Commit
88a6df7
·
verified ·
1 Parent(s): bd38203

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -45
app.py CHANGED
@@ -1,10 +1,13 @@
1
- #Importing required packages
2
  import streamlit as st
3
- import anthropic
4
  import uuid
 
 
 
 
5
 
6
  INIT_PROMPT = """
7
- \n\nHuman: You are MapMentor a trainer in Wardley Mapping. You will help the users learn about Wardley Mapping
8
  """
9
 
10
  TRAINING_PROMPT = """
@@ -12,40 +15,34 @@ Here is an outline for a training course that you will give to the user. It cove
12
  """
13
 
14
  INTRO_PROMPT = """
15
- Hello, I'm DarijaBot
16
-
17
  """
18
 
19
  REG_PROMPT = """
20
- \n\nHuman: Here is the user's question about Wardley Mapping:
21
  <question>
22
  {QUESTION}
23
  </question>
24
- \n\nAssistant: [MapMentor] <response>
25
  """
26
 
27
- MODEL = "claude-3-5-sonnet-20240620"
28
-
29
  new_prompt = []
30
 
31
  if "session_id" not in st.session_state:
32
  st.session_state.session_id = str(uuid.uuid4())
33
 
34
- st.set_page_config(page_title="Anthropic - ChatBot")
35
- st.sidebar.title("Anthropic - ChatBot")
36
  st.sidebar.title("Wardley Mapping Mentor")
37
  st.sidebar.divider()
38
- st.sidebar.markdown("Developed by Bahae Eddine HALIM](https://linkedin.com/in/halimbahae)", unsafe_allow_html=True)
39
  st.sidebar.markdown("Current Version: 0.0.4")
40
- st.sidebar.markdown("Using claude-2 API")
41
  st.sidebar.markdown(st.session_state.session_id)
42
  st.sidebar.divider()
43
 
44
  # Check if the user has provided an API key, otherwise default to the secret
45
- user_claude_api_key = st.sidebar.text_input("Enter your Anthropic API Key:", placeholder="sk-...", type="password")
46
-
47
- if "claude_model" not in st.session_state:
48
- st.session_state["claude_model"] = MODEL
49
 
50
  if "messages" not in st.session_state:
51
  st.session_state["messages"] = []
@@ -54,14 +51,10 @@ if "messages" not in st.session_state:
54
  if "all_prompts" not in st.session_state:
55
  st.session_state["all_prompts"] = INIT_PROMPT + TRAINING_PROMPT
56
 
57
- if user_claude_api_key:
58
- # If the user has provided an API key, use it
59
- client=anthropic.Anthropic(
60
- # defaults to os.environ.get("ANTHROPIC_API_KEY")
61
- api_key=user_claude_api_key,
62
- )
63
  else:
64
- st.warning("Please enter your Anthropic Claude API key", icon="⚠️")
65
 
66
  for message in st.session_state.messages:
67
  if message["role"] in ["user", "assistant"]:
@@ -69,9 +62,9 @@ for message in st.session_state.messages:
69
  new_prompt.append(message["content"])
70
  st.markdown(message["content"])
71
 
72
- if user_claude_api_key:
73
- if user_input := st.chat_input("How can I help with Wardley Mapping?"):
74
- prompt = REG_PROMPT.format(QUESTION = user_input)
75
  st.session_state.all_prompts += prompt
76
  st.session_state.messages.append({"role": "user", "content": user_input})
77
  with st.chat_message("user"):
@@ -80,24 +73,16 @@ if user_claude_api_key:
80
  message_placeholder = st.empty()
81
  full_response = ""
82
  try:
83
- for response in client.completions.create(
84
- prompt=st.session_state.all_prompts,
85
- stop_sequences=["</response>"],
86
- model=MODEL,
87
- max_tokens_to_sample=500,
88
- stream=True,
89
- ):
90
- full_response += response.completion
91
- message_placeholder.markdown(full_response + "▌")
92
  message_placeholder.markdown(full_response)
93
- except anthropic.APIConnectionError as e:
94
- st.error("The server could not be reached")
95
- print(e.__cause__) # an underlying Exception, likely raised within httpx.
96
- except anthropic.RateLimitError as e:
97
- st.error("A 429 status code was received; we should back off a bit.")
98
- except anthropic.APIStatusError as e:
99
- st.error("Another non-200-range status code was received\nTry again later.")
100
- st.error(e.status_code)
101
- st.error(e.response)
102
  st.session_state.messages.append({"role": "assistant", "content": full_response})
103
  st.session_state.all_prompts += full_response
 
1
+ # Importing required packages
2
  import streamlit as st
 
3
  import uuid
4
+ from huggingface_hub import InferenceClient
5
+
6
+ # Initialize the HuggingFace inference client
7
+ client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta", token=st.secrets["HUGGINGFACE_API_KEY"])
8
 
9
  INIT_PROMPT = """
10
+ \n\nHuman: You are DarijaBot, a helpful assistant that can converse in both Latin and Arabic alphabet Darija. You will help the users learn about Wardley Mapping.
11
  """
12
 
13
  TRAINING_PROMPT = """
 
15
  """
16
 
17
  INTRO_PROMPT = """
18
+ Hello, I'm DarijaBot! I can help you with your questions about Wardley Mapping in Darija. You can type in either Latin or Arabic script.
 
19
  """
20
 
21
  REG_PROMPT = """
22
+ \n\nHuman: Here is the user's question in Darija:
23
  <question>
24
  {QUESTION}
25
  </question>
26
+ \n\nAssistant: [DarijaBot] <response>
27
  """
28
 
 
 
29
  new_prompt = []
30
 
31
  if "session_id" not in st.session_state:
32
  st.session_state.session_id = str(uuid.uuid4())
33
 
34
+ st.set_page_config(page_title="DarijaBot - ChatBot")
35
+ st.sidebar.title("DarijaBot - ChatBot")
36
  st.sidebar.title("Wardley Mapping Mentor")
37
  st.sidebar.divider()
38
+ st.sidebar.markdown("Developed by [Bahae Eddine HALIM](https://linkedin.com/in/halimbahae)", unsafe_allow_html=True)
39
  st.sidebar.markdown("Current Version: 0.0.4")
40
+ st.sidebar.markdown("Using HuggingFaceH4/zephyr-7b-beta API")
41
  st.sidebar.markdown(st.session_state.session_id)
42
  st.sidebar.divider()
43
 
44
  # Check if the user has provided an API key, otherwise default to the secret
45
+ user_huggingface_api_key = st.sidebar.text_input("Enter your HuggingFace API Key:", placeholder="hf_...", type="password")
 
 
 
46
 
47
  if "messages" not in st.session_state:
48
  st.session_state["messages"] = []
 
51
  if "all_prompts" not in st.session_state:
52
  st.session_state["all_prompts"] = INIT_PROMPT + TRAINING_PROMPT
53
 
54
+ if user_huggingface_api_key:
55
+ client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta", token=user_huggingface_api_key)
 
 
 
 
56
  else:
57
+ st.warning("Please enter your HuggingFace API key", icon="⚠️")
58
 
59
  for message in st.session_state.messages:
60
  if message["role"] in ["user", "assistant"]:
 
62
  new_prompt.append(message["content"])
63
  st.markdown(message["content"])
64
 
65
+ if user_huggingface_api_key:
66
+ if user_input := st.chat_input("How can I help with Wardley Mapping? (Darija: Latin or Arabic script)"):
67
+ prompt = REG_PROMPT.format(QUESTION=user_input)
68
  st.session_state.all_prompts += prompt
69
  st.session_state.messages.append({"role": "user", "content": user_input})
70
  with st.chat_message("user"):
 
73
  message_placeholder = st.empty()
74
  full_response = ""
75
  try:
76
+ response = client.chat_completion(
77
+ [{"role": "system", "content": "You are DarijaBot, a helpful assistant that can converse in both Latin and Arabic alphabet Darija."}, {"role": "user", "content": user_input}],
78
+ max_tokens=512,
79
+ temperature=0.7,
80
+ top_p=0.95
81
+ ).choices[0].message.content
82
+ full_response += response
83
+ message_placeholder.markdown(full_response + "▌")
 
84
  message_placeholder.markdown(full_response)
85
+ except Exception as e:
86
+ st.error(f"Error: Unable to fetch response from the API.\nDetails: {e}")
 
 
 
 
 
 
 
87
  st.session_state.messages.append({"role": "assistant", "content": full_response})
88
  st.session_state.all_prompts += full_response