accesscreate012's picture
Update app.py
b32f6e7 verified
import gradio as gr
from huggingface_hub import InferenceClient
from datasets import load_dataset
import threading
import time
import os
# Get Hugging Face API token from secrets
API_TOKEN = os.getenv("token")
if not API_TOKEN:
print("ERROR: API token not found!")
else:
print("API token retrieved successfully.")
# Initialize inference client with Zephyr-7B
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=API_TOKEN)
def load_data():
"""Load dataset from Hugging Face and store it in a dictionary."""
dataset = load_dataset("accesscreate012/abhinav-academy-chatbot", split="train")
return {entry["instruction"].strip(): entry["response"].strip() for entry in dataset}
# Global dataset
data = load_data()
def auto_update():
"""Automatically refresh the dataset every 24 hours."""
global data
while True:
time.sleep(86400) # 24 hours
data = load_data()
print("Dataset updated.")
# Start dataset auto-update in a separate thread
threading.Thread(target=auto_update, daemon=True).start()
def respond(
message,
history: list[tuple[str, str]],
system_message,
max_tokens,
temperature,
top_p,
):
print("Received message:", message)
# Check if the message matches an entry in the dataset
if message.strip() in data:
print("Found exact match in dataset.")
yield data[message.strip()] # Return the exact response from the dataset
return
print("No exact match found, using Zephyr-7B.")
# Construct system message with dataset context
dataset_context = "\n".join([f"Q: {q}\nA: {a}" for q, a in data.items()])
full_system_message = (
f"{system_message}\n\n"
"Only use the following dataset for answers:\n"
f"{dataset_context}\n"
"If the exact answer is not found, infer based on the data.\n"
"Do NOT generate unrelated information.\n"
"Keep responses short and accurate."
)
# Construct conversation history
messages = [{"role": "system", "content": full_system_message}]
for user_input, bot_response in history:
if user_input:
messages.append({"role": "user", "content": user_input})
if bot_response:
messages.append({"role": "assistant", "content": bot_response})
messages.append({"role": "user", "content": message})
response = ""
try:
for msg in client.chat_completion(
messages,
max_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
):
token = msg.choices[0].delta.content
response += token
yield response
except Exception as e:
print("Error during chat completion:", str(e))
yield "An error occurred: " + str(e)
# Gradio Chat UI
demo = gr.ChatInterface(
respond,
additional_inputs=[
gr.Textbox(value="You are a helpful and knowledgeable chatbot.", label="System message"),
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max tokens"),
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
],
)
if __name__ == "__main__":
demo.launch()