Spaces:
Sleeping
Sleeping
File size: 3,331 Bytes
b05da07 162f57a ba99459 b32f6e7 ba99459 b05da07 4f217c8 b32f6e7 ba99459 b05da07 f35e7cd 162f57a b32f6e7 162f57a b32f6e7 b05da07 f35e7cd b32f6e7 162f57a f35e7cd 162f57a b32f6e7 162f57a b32f6e7 162f57a f35e7cd b32f6e7 162f57a f35e7cd f2c3ae5 c1faf04 b32f6e7 c1faf04 b32f6e7 c1faf04 b32f6e7 f2c3ae5 b32f6e7 f35e7cd b32f6e7 f35e7cd b05da07 f2c3ae5 b32f6e7 f2c3ae5 b32f6e7 f2c3ae5 f35e7cd b32f6e7 f35e7cd b32f6e7 c6251cb b32f6e7 b05da07 f35e7cd b05da07 b32f6e7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
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()
|