|
|
|
|
|
import warnings |
|
warnings.filterwarnings("ignore") |
|
|
|
|
|
from dotenv import load_dotenv |
|
import os |
|
import gradio as gr |
|
from huggingface_hub import InferenceClient |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
HUGGINGFACE_API_KEY = os.getenv("HUGGINGFACE_API_KEY") |
|
if not HUGGINGFACE_API_KEY: |
|
raise ValueError("HUGGINGFACE_API_KEY is not set in environment variables or Spaces secrets") |
|
|
|
|
|
client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta", token=HUGGINGFACE_API_KEY) |
|
|
|
|
|
PERSONALITY_FILE = "personality.txt" |
|
try: |
|
with open(PERSONALITY_FILE, "r") as f: |
|
personality_context = f.read() |
|
except FileNotFoundError: |
|
personality_context = "Default personality: A friendly and witty chatbot with a passion for horror and gaming." |
|
warnings.warn(f"Personality file not found at {PERSONALITY_FILE}. Using default personality.") |
|
|
|
def respond( |
|
message: str, |
|
history: list[tuple[str, str]], |
|
system_message: str, |
|
max_tokens: int, |
|
temperature: float, |
|
top_p: float, |
|
): |
|
""" |
|
Generate a response using the Hugging Face Inference API with RAG to enforce |
|
the ZombieSlayerBot personality defined in personality.txt. |
|
""" |
|
if not message.strip(): |
|
return "Please say something, survivor! The zombies are waiting!" |
|
|
|
|
|
message_lower = message.lower().strip() |
|
greetings = ["hi", "hello", "hey", "good morning", "good afternoon"] |
|
if any(greeting in message_lower for greeting in greetings): |
|
yield "Yo, survivor! Ready to dive into the zombie-infested chaos of Raccoon City? What's up?" |
|
return |
|
|
|
|
|
full_system_message = ( |
|
f"{system_message}\n\n" |
|
"Follow this personality profile in all responses:\n" |
|
f"{personality_context}\n\n" |
|
"Use the conversation history and the user's message to generate a response that aligns with the personality." |
|
) |
|
|
|
|
|
messages = [{"role": "system", "content": full_system_message}] |
|
for user_msg, bot_msg in history: |
|
if user_msg: |
|
messages.append({"role": "user", "content": user_msg}) |
|
if bot_msg: |
|
messages.append({"role": "assistant", "content": bot_msg}) |
|
messages.append({"role": "user", "content": message}) |
|
|
|
|
|
response = "" |
|
try: |
|
for message_chunk in client.chat_completion( |
|
messages, |
|
max_tokens=max_tokens, |
|
stream=True, |
|
temperature=temperature, |
|
top_p=top_p, |
|
): |
|
token = message_chunk.choices[0].delta.content or "" |
|
response += token |
|
yield response |
|
except Exception as e: |
|
yield f"Error in the apocalypse: {str(e)}. Try again, survivor!" |
|
|
|
|
|
def create_chatbot(): |
|
with gr.Blocks(title="ZombieSlayerBot") as demo: |
|
gr.Markdown("# 🧟♂️ ZombieSlayerBot") |
|
gr.Markdown("Welcome, survivor! I'm ZombieSlayerBot, your guide through the zombie-infested world of Resident Evil. Powered by Hugging Face's Zephyr-7B-Beta. Let’s lock and load—chat with me!") |
|
|
|
|
|
chat_interface = gr.ChatInterface( |
|
fn=respond, |
|
chatbot=gr.Chatbot(height=400, show_label=False, container=True), |
|
textbox=gr.Textbox(placeholder="Type your message here, survivor...", container=False, scale=4), |
|
additional_inputs=[ |
|
gr.Textbox(value="You are ZombieSlayerBot, a witty and bold chatbot obsessed with Resident Evil.", label="System message"), |
|
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new 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 (nucleus sampling)"), |
|
], |
|
submit_btn=gr.Button("Send", variant="primary"), |
|
) |
|
|
|
|
|
clear_btn = gr.Button("Clear Chat", variant="secondary") |
|
clear_btn.click(lambda: None, None, chat_interface.chatbot, queue=False) |
|
|
|
return demo |
|
|
|
if __name__ == "__main__": |
|
demo = create_chatbot() |
|
demo.launch(debug=False) |