Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,37 +5,38 @@ import threading
|
|
| 5 |
import time
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
#
|
| 9 |
API_TOKEN = os.getenv("token")
|
| 10 |
|
| 11 |
if not API_TOKEN:
|
| 12 |
print("ERROR: API token not found!")
|
| 13 |
else:
|
| 14 |
print("API token retrieved successfully.")
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
"""
|
| 18 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=API_TOKEN)
|
| 19 |
|
| 20 |
|
| 21 |
def load_data():
|
|
|
|
| 22 |
dataset = load_dataset("accesscreate012/abhinav-academy-chatbot", split="train")
|
| 23 |
-
return {entry["instruction"]: entry["response"] for entry in dataset}
|
| 24 |
|
| 25 |
|
| 26 |
-
# Global
|
| 27 |
data = load_data()
|
| 28 |
|
| 29 |
|
| 30 |
def auto_update():
|
|
|
|
| 31 |
global data
|
| 32 |
while True:
|
| 33 |
-
time.sleep(86400) #
|
| 34 |
-
data = load_data()
|
| 35 |
print("Dataset updated.")
|
| 36 |
|
| 37 |
|
| 38 |
-
# Start
|
| 39 |
threading.Thread(target=auto_update, daemon=True).start()
|
| 40 |
|
| 41 |
|
|
@@ -49,35 +50,47 @@ def respond(
|
|
| 49 |
):
|
| 50 |
print("Received message:", message)
|
| 51 |
|
| 52 |
-
# Check if the
|
| 53 |
-
if message in data:
|
| 54 |
print("Found exact match in dataset.")
|
| 55 |
-
yield data[message] # Return the exact response from the dataset
|
| 56 |
return
|
| 57 |
|
| 58 |
-
print("No exact match found, using
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
|
|
|
| 61 |
|
| 62 |
-
for
|
| 63 |
-
if
|
| 64 |
-
messages.append({"role": "user", "content":
|
| 65 |
-
if
|
| 66 |
-
messages.append({"role": "assistant", "content":
|
| 67 |
|
| 68 |
messages.append({"role": "user", "content": message})
|
| 69 |
|
| 70 |
response = ""
|
| 71 |
|
| 72 |
try:
|
| 73 |
-
for
|
| 74 |
messages,
|
| 75 |
max_tokens=max_tokens,
|
| 76 |
stream=True,
|
| 77 |
temperature=temperature,
|
| 78 |
top_p=top_p,
|
| 79 |
):
|
| 80 |
-
token =
|
| 81 |
response += token
|
| 82 |
yield response
|
| 83 |
except Exception as e:
|
|
@@ -85,25 +98,17 @@ def respond(
|
|
| 85 |
yield "An error occurred: " + str(e)
|
| 86 |
|
| 87 |
|
| 88 |
-
|
| 89 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 90 |
-
"""
|
| 91 |
demo = gr.ChatInterface(
|
| 92 |
respond,
|
| 93 |
additional_inputs=[
|
| 94 |
-
gr.Textbox(value="You are a
|
| 95 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max
|
| 96 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 97 |
-
gr.Slider(
|
| 98 |
-
minimum=0.1,
|
| 99 |
-
maximum=1.0,
|
| 100 |
-
value=0.95,
|
| 101 |
-
step=0.05,
|
| 102 |
-
label="Top-p (nucleus sampling)",
|
| 103 |
-
),
|
| 104 |
],
|
| 105 |
)
|
| 106 |
|
| 107 |
|
| 108 |
if __name__ == "__main__":
|
| 109 |
-
demo.launch()
|
|
|
|
| 5 |
import time
|
| 6 |
import os
|
| 7 |
|
| 8 |
+
# Get Hugging Face API token from secrets
|
| 9 |
API_TOKEN = os.getenv("token")
|
| 10 |
|
| 11 |
if not API_TOKEN:
|
| 12 |
print("ERROR: API token not found!")
|
| 13 |
else:
|
| 14 |
print("API token retrieved successfully.")
|
| 15 |
+
|
| 16 |
+
# Initialize inference client with Zephyr-7B
|
|
|
|
| 17 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=API_TOKEN)
|
| 18 |
|
| 19 |
|
| 20 |
def load_data():
|
| 21 |
+
"""Load dataset from Hugging Face and store it in a dictionary."""
|
| 22 |
dataset = load_dataset("accesscreate012/abhinav-academy-chatbot", split="train")
|
| 23 |
+
return {entry["instruction"].strip(): entry["response"].strip() for entry in dataset}
|
| 24 |
|
| 25 |
|
| 26 |
+
# Global dataset
|
| 27 |
data = load_data()
|
| 28 |
|
| 29 |
|
| 30 |
def auto_update():
|
| 31 |
+
"""Automatically refresh the dataset every 24 hours."""
|
| 32 |
global data
|
| 33 |
while True:
|
| 34 |
+
time.sleep(86400) # 24 hours
|
| 35 |
+
data = load_data()
|
| 36 |
print("Dataset updated.")
|
| 37 |
|
| 38 |
|
| 39 |
+
# Start dataset auto-update in a separate thread
|
| 40 |
threading.Thread(target=auto_update, daemon=True).start()
|
| 41 |
|
| 42 |
|
|
|
|
| 50 |
):
|
| 51 |
print("Received message:", message)
|
| 52 |
|
| 53 |
+
# Check if the message matches an entry in the dataset
|
| 54 |
+
if message.strip() in data:
|
| 55 |
print("Found exact match in dataset.")
|
| 56 |
+
yield data[message.strip()] # Return the exact response from the dataset
|
| 57 |
return
|
| 58 |
|
| 59 |
+
print("No exact match found, using Zephyr-7B.")
|
| 60 |
+
|
| 61 |
+
# Construct system message with dataset context
|
| 62 |
+
dataset_context = "\n".join([f"Q: {q}\nA: {a}" for q, a in data.items()])
|
| 63 |
+
full_system_message = (
|
| 64 |
+
f"{system_message}\n\n"
|
| 65 |
+
"Only use the following dataset for answers:\n"
|
| 66 |
+
f"{dataset_context}\n"
|
| 67 |
+
"If the exact answer is not found, infer based on the data.\n"
|
| 68 |
+
"Do NOT generate unrelated information.\n"
|
| 69 |
+
"Keep responses short and accurate."
|
| 70 |
+
)
|
| 71 |
|
| 72 |
+
# Construct conversation history
|
| 73 |
+
messages = [{"role": "system", "content": full_system_message}]
|
| 74 |
|
| 75 |
+
for user_input, bot_response in history:
|
| 76 |
+
if user_input:
|
| 77 |
+
messages.append({"role": "user", "content": user_input})
|
| 78 |
+
if bot_response:
|
| 79 |
+
messages.append({"role": "assistant", "content": bot_response})
|
| 80 |
|
| 81 |
messages.append({"role": "user", "content": message})
|
| 82 |
|
| 83 |
response = ""
|
| 84 |
|
| 85 |
try:
|
| 86 |
+
for msg in client.chat_completion(
|
| 87 |
messages,
|
| 88 |
max_tokens=max_tokens,
|
| 89 |
stream=True,
|
| 90 |
temperature=temperature,
|
| 91 |
top_p=top_p,
|
| 92 |
):
|
| 93 |
+
token = msg.choices[0].delta.content
|
| 94 |
response += token
|
| 95 |
yield response
|
| 96 |
except Exception as e:
|
|
|
|
| 98 |
yield "An error occurred: " + str(e)
|
| 99 |
|
| 100 |
|
| 101 |
+
# Gradio Chat UI
|
|
|
|
|
|
|
| 102 |
demo = gr.ChatInterface(
|
| 103 |
respond,
|
| 104 |
additional_inputs=[
|
| 105 |
+
gr.Textbox(value="You are a helpful and knowledgeable chatbot.", label="System message"),
|
| 106 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max tokens"),
|
| 107 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 108 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
],
|
| 110 |
)
|
| 111 |
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|
| 114 |
+
demo.launch()
|