Illia56 commited on
Commit
a197dc7
β€’
1 Parent(s): 9978831

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -34
app.py CHANGED
@@ -2,63 +2,64 @@ import streamlit as st
2
  from gradio_client import Client
3
 
4
  # Constants
5
- APP_TITLE = "Llama2 70B Chatbot"
6
- APP_DESCRIPTION = """
7
- This application demonstrates the Llama-2-70b chatbot model by Meta,
8
- fine-tuned for chat instructions. You can interact with the model and ask questions.
9
  """
10
 
11
  # Initialize client
12
- llama2_client = Client("https://ysharma-explore-llamav2-with-tgi.hf.space/")
 
13
 
14
  with st.sidebar:
15
- system_prompt_input = st.text_input("Optional system prompt:")
16
- temperature_slider = st.slider("Temperature", min_value=0.0, max_value=1.0, value=0.9, step=0.05)
17
- max_new_tokens_slider = st.slider("Max new tokens", min_value=0.0, max_value=4096.0, value=4096.0, step=64.0)
 
 
 
18
 
 
19
  # Prediction function
20
- def get_llama2_response(user_message, system_prompt, temperature, max_new_tokens, topp=0.6, repetition_penalty=1.2):
21
- with st.status("Requesting Llama-2"):
22
- st.write("Requesting API...")
23
- response = llama2_client.predict(
24
- user_message,
25
- system_prompt,
26
- temperature,
27
- max_new_tokens,
28
- topp,
29
- repetition_penalty,
30
- api_name="/chat"
31
  )
32
  st.write("Done")
33
  return response
34
 
35
  # Streamlit UI
36
- st.title(APP_TITLE)
37
- st.write(APP_DESCRIPTION)
 
38
 
39
- if "chat_history" not in st.session_state:
40
- st.session_state.chat_history = []
41
 
42
  # Display chat messages from history on app rerun
43
- for message in st.session_state.chat_history:
44
  with st.chat_message(message["role"]):
45
  st.markdown(message["content"])
46
 
47
  # React to user input
48
- if user_input := st.chat_input("Ask Llama-2-70B anything..."):
49
  # Display user message in chat message container
50
- st.chat_message("user", avatar="πŸ§‘β€πŸ’»").markdown(user_input)
51
  # Add user message to chat history
52
- st.session_state.chat_history.append({"role": "user", "content": user_input})
53
 
54
- response = get_llama2_response(
55
- user_input,
56
- system_prompt_input,
57
- temperature_slider,
58
- max_new_tokens_slider
59
- )
60
  # Display assistant response in chat message container
61
  with st.chat_message("assistant", avatar='πŸ¦™'):
62
  st.markdown(response)
63
  # Add assistant response to chat history
64
- st.session_state.chat_history.append({"role": "assistant", "content": response})
 
2
  from gradio_client import Client
3
 
4
  # Constants
5
+ TITLE = "Llama2 70B Chatbot"
6
+ DESCRIPTION = """
7
+ This Space demonstrates model [Llama-2-70b-chat-hf](https://huggingface.co/meta-llama/Llama-2-70b-chat-hf) by Meta,
8
+ a Llama 2 model with 70B parameters fine-tuned for chat instructions.
9
  """
10
 
11
  # Initialize client
12
+ client = Client("https://ysharma-explore-llamav2-with-tgi.hf.space/")
13
+
14
 
15
  with st.sidebar:
16
+ system_promptSide = st.text_input("Optional system prompt:")
17
+ temperatureSide = st.slider("Temperature", min_value=0.0, max_value=1.0, value=0.9, step=0.05)
18
+ max_new_tokensSide = st.slider("Max new tokens", min_value=0.0, max_value=4096.0, value=4096.0, step=64.0)
19
+ ToppSide = st.slider("Top-p (nucleus sampling)", min_value=0.0, max_value=1.0, value=0.6, step=0.05)
20
+ RepetitionpenaltySide = st.slider("Repetition penalty", min_value=0.0, max_value=2.0, value=1.2, step=0.05)
21
+
22
 
23
+
24
  # Prediction function
25
+ def predict(message, system_prompt, temperature, max_new_tokens,Topp,Repetitionpenalty):
26
+ with st.status("Requesting LLama-2"):
27
+ st.write("Requesting API")
28
+ response = client.predict(
29
+ message, # str in 'Message' Textbox component
30
+ system_prompt, # str in 'Optional system prompt' Textbox component
31
+ temperature, # int | float (numeric value between 0.0 and 1.0)
32
+ max_new_tokens, # int | float (numeric value between 0 and 4096)
33
+ Topp, # int | float (numeric value between 0.0 and 1)
34
+ Repetitionpenalty, # int | float (numeric value between 1.0 and 2.0)
35
+ api_name="/chat"
36
  )
37
  st.write("Done")
38
  return response
39
 
40
  # Streamlit UI
41
+ st.title(TITLE)
42
+ st.write(DESCRIPTION)
43
+
44
 
45
+ if "messages" not in st.session_state:
46
+ st.session_state.messages = []
47
 
48
  # Display chat messages from history on app rerun
49
+ for message in st.session_state.messages:
50
  with st.chat_message(message["role"]):
51
  st.markdown(message["content"])
52
 
53
  # React to user input
54
+ if prompt := st.chat_input("Ask LLama-2-70b anything..."):
55
  # Display user message in chat message container
56
+ st.chat_message("human",avatar = "πŸ§‘β€πŸ’»").markdown(prompt)
57
  # Add user message to chat history
58
+ st.session_state.messages.append({"role": "human", "content": prompt})
59
 
60
+ response = predict(prompt,system_promptSide,temperatureSide,max_new_tokensSide,ToppSide,RepetitionpenaltySide)
 
 
 
 
 
61
  # Display assistant response in chat message container
62
  with st.chat_message("assistant", avatar='πŸ¦™'):
63
  st.markdown(response)
64
  # Add assistant response to chat history
65
+ st.session_state.messages.append({"role": "assistant", "content": response})