abanm commited on
Commit
082f600
·
verified ·
1 Parent(s): e309dbc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from huggingface_hub import InferenceClient
3
+
4
+ # Constants
5
+ SPACE_URL = "https://z7svds7k42bwhhgm.us-east-1.aws.endpoints.huggingface.cloud"
6
+ HF_API_KEY = HF_API_KEY = os.getenv("HF_API_KEY")
7
+ DUBS_PATH = "🐾" # Optional: Replace with an avatar path if needed
8
+
9
+ # Streamlit Configuration
10
+ st.set_page_config(page_title="Chatbot Test", page_icon="🤖", layout="centered")
11
+
12
+ # Chat State
13
+ if "messages" not in st.session_state:
14
+ st.session_state["messages"] = []
15
+
16
+ # Function to Stream Response
17
+ def stream_response(prompt_text, api_key):
18
+ """
19
+ Stream text from the HF Inference Endpoint using the InferenceClient.
20
+ Yields each partial chunk of text as it arrives.
21
+ """
22
+ client = InferenceClient(SPACE_URL, token=api_key)
23
+
24
+ gen_kwargs = {
25
+ "max_new_tokens": 512,
26
+ "top_k": 30,
27
+ "top_p": 0.9,
28
+ "temperature": 0.2,
29
+ "repetition_penalty": 1.02,
30
+ "stop_sequences": ["<|endoftext|>"]
31
+ }
32
+
33
+ stream = client.text_generation(prompt_text, stream=True, details=True, **gen_kwargs)
34
+
35
+ try:
36
+ for response in stream:
37
+ if response.token.special:
38
+ continue
39
+ yield response.token.text
40
+ except Exception as e:
41
+ yield f"Error: {e}"
42
+
43
+ # Streamlit Chat Interface
44
+ st.title("Chatbot Testing Interface")
45
+
46
+ # User Input Section
47
+ prompt = st.chat_input("Enter your message...")
48
+
49
+ if prompt:
50
+ # 1) Add the user's message to session state
51
+ st.session_state["messages"].append({"role": "user", "content": prompt})
52
+ st.chat_message("user").write(prompt)
53
+
54
+ # 2) Build combined chat history for the model prompt
55
+ chat_history = "".join(
56
+ [f"<|{msg['role']}|>{msg['content']}<|end|>" for msg in st.session_state["messages"]]
57
+ )
58
+
59
+ # 3) Generate the response
60
+ with st.chat_message("assistant", avatar=DUBS_PATH):
61
+ with st.spinner("Dubs is thinking... Woof Woof! 🐾"):
62
+ full_response = ""
63
+ placeholder = st.empty() # Placeholder for streaming response
64
+ response = stream_response(chat_history, HF_API_KEY)
65
+ for item in response:
66
+ full_response += item
67
+ placeholder.markdown(full_response)
68
+ placeholder.markdown(full_response)
69
+
70
+ # 4) Add assistant response to the session state
71
+ st.session_state["messages"].append({"role": "assistant", "content": full_response})