ashok2216 commited on
Commit
aa0b46e
·
verified ·
1 Parent(s): e3c90a7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from huggingface_hub import InferenceClient
3
+
4
+ # Initialize the InferenceClient
5
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
6
+
7
+ def respond(
8
+ message,
9
+ history: list[tuple[str, str]],
10
+ system_message,
11
+ max_tokens,
12
+ temperature,
13
+ top_p,
14
+ ):
15
+ messages = [{"role": "system", "content": system_message}]
16
+
17
+ for val in history:
18
+ if val[0]:
19
+ messages.append({"role": "user", "content": val[0]})
20
+ if val[1]:
21
+ messages.append({"role": "assistant", "content": val[1]})
22
+
23
+ messages.append({"role": "user", "content": message})
24
+
25
+ response = ""
26
+ response_container = st.empty() # Placeholder to update the response text
27
+
28
+ for message in client.chat_completion(
29
+ messages,
30
+ max_tokens=max_tokens,
31
+ stream=True,
32
+ temperature=temperature,
33
+ top_p=top_p,
34
+ ):
35
+ token = message.choices[0].delta.content
36
+
37
+ response += token
38
+ response_container.text(response) # Dynamically update the response
39
+
40
+ # Streamlit interface
41
+ st.title("Chatbot Interface")
42
+
43
+ # User inputs
44
+ system_message = st.text_input("System message", value="You are a friendly Chatbot.")
45
+ message = st.text_input("User message", key="user_message")
46
+ max_tokens = st.slider("Max new tokens", 1, 2048, 512)
47
+ temperature = st.slider("Temperature", 0.1, 4.0, 0.7)
48
+ top_p = st.slider("Top-p (nucleus sampling)", 0.1, 1.0, 0.95)
49
+
50
+ # A placeholder for chat history
51
+ history = st.session_state.get('history', [])
52
+
53
+ # Submit button
54
+ if st.button("Send"):
55
+ if message:
56
+ # Append the new message to the history
57
+ history.append((message, "")) # No assistant response yet
58
+
59
+ # Call the respond function and update the history with the assistant's reply
60
+ for response in respond(message, history, system_message, max_tokens, temperature, top_p):
61
+ history[-1] = (message, response)
62
+
63
+ # Save updated history in session state
64
+ st.session_state['history'] = history
65
+
66
+ # Display the chat history
67
+ for user_msg, bot_msg in history:
68
+ st.write(f"**User**: {user_msg}")
69
+ st.write(f"**Assistant**: {bot_msg}")