Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,10 @@
|
|
| 1 |
-
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
-
import os
|
| 4 |
-
|
| 5 |
-
# Constants
|
| 6 |
-
SPACE_URL = "https://z7svds7k42bwhhgm.us-east-1.aws.endpoints.huggingface.cloud"
|
| 7 |
-
HF_API_KEY = os.getenv("HF_API_KEY")
|
| 8 |
-
DUBS_PATH = "🐾" # Optional: Replace with an avatar path if needed
|
| 9 |
-
|
| 10 |
-
# Streamlit Configuration
|
| 11 |
-
st.set_page_config(page_title="Chatbot Test", page_icon="🤖", layout="centered")
|
| 12 |
-
|
| 13 |
-
client = InferenceClient(SPACE_URL, token=HF_API_KEY)
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
# Function to Fetch Response (Non-Streaming)
|
| 20 |
-
def fetch_response(prompt_text):
|
| 21 |
"""
|
| 22 |
Fetch full text response from the HF Inference Endpoint using the InferenceClient.
|
| 23 |
-
Returns
|
| 24 |
"""
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
gen_kwargs = {
|
| 28 |
"max_new_tokens": 512,
|
| 29 |
"top_k": 30,
|
| 30 |
"top_p": 0.9,
|
|
@@ -32,37 +12,14 @@ def fetch_response(prompt_text):
|
|
| 32 |
"repetition_penalty": 1.02,
|
| 33 |
"stop_sequences": ["<|endoftext|>"]
|
| 34 |
}
|
| 35 |
-
stream = client.text_generation(prompt, stream=True, details=True, **gen_kwargs)
|
| 36 |
-
|
| 37 |
-
for response in stream:
|
| 38 |
-
if response.token.special:
|
| 39 |
-
continue
|
| 40 |
-
# Break if we encounter a stop sequence
|
| 41 |
-
if response.token.text in gen_kwargs["stop_sequences"]:
|
| 42 |
-
break
|
| 43 |
-
# Append the new token's text to the partial text
|
| 44 |
-
partial_text += response.token.text
|
| 45 |
-
yield response.token.text # Yield only the new chunk
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# Streamlit Chat Interface
|
| 52 |
-
st.title("Chatbot Testing Interface")
|
| 53 |
-
|
| 54 |
-
# User Input Section
|
| 55 |
-
prompt = st.chat_input("Enter your message...")
|
| 56 |
-
|
| 57 |
-
if prompt:
|
| 58 |
-
# Display the user's message
|
| 59 |
-
st.chat_message("user").write(prompt)
|
| 60 |
|
| 61 |
-
|
| 62 |
-
chat_history = f"<|user|>{prompt}<|end|> \n <|assistant|> "
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def fetch_response(prompt):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
"""
|
| 3 |
Fetch full text response from the HF Inference Endpoint using the InferenceClient.
|
| 4 |
+
Returns tokens in a streaming fashion.
|
| 5 |
"""
|
| 6 |
+
partial_text = ""
|
| 7 |
+
gen_kwargs = {
|
|
|
|
| 8 |
"max_new_tokens": 512,
|
| 9 |
"top_k": 30,
|
| 10 |
"top_p": 0.9,
|
|
|
|
| 12 |
"repetition_penalty": 1.02,
|
| 13 |
"stop_sequences": ["<|endoftext|>"]
|
| 14 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
stream = client.text_generation(prompt, stream=True, details=True, **gen_kwargs)
|
|
|
|
| 17 |
|
| 18 |
+
for response in stream:
|
| 19 |
+
if response.token.special:
|
| 20 |
+
continue
|
| 21 |
+
# Stop if we encounter stop_sequences
|
| 22 |
+
if response.token.text in gen_kwargs["stop_sequences"]:
|
| 23 |
+
break
|
| 24 |
+
partial_text += response.token.text
|
| 25 |
+
yield response.token.text
|