Update app.py
Browse files
app.py
CHANGED
|
@@ -7,115 +7,58 @@ For more information on `huggingface_hub` Inference API support, please check th
|
|
| 7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
import os
|
| 26 |
-
import gradio as gr
|
| 27 |
-
from together import Together
|
| 28 |
|
| 29 |
-
# Load environment variables from .env file
|
| 30 |
-
load_dotenv("/Users/ay/Documents/projects/Agentic AI hackathon/.env")
|
| 31 |
-
|
| 32 |
-
# Get the API key from environment variables
|
| 33 |
-
TOGETHER_API_KEY = os.getenv("TOGETHER_API_KEY")
|
| 34 |
-
if not TOGETHER_API_KEY:
|
| 35 |
-
raise ValueError("TOGETHER_API_KEY is not set in the .env file at /Users/ay/Documents/projects/Agentic AI hackathon/.env")
|
| 36 |
-
|
| 37 |
-
# Initialize the Together client
|
| 38 |
-
client = Together(api_key=TOGETHER_API_KEY)
|
| 39 |
-
|
| 40 |
-
# Load personality context from file
|
| 41 |
-
PERSONALITY_FILE = "/Users/ay/Documents/projects/Agentic AI hackathon/personality.txt"
|
| 42 |
-
try:
|
| 43 |
-
with open(PERSONALITY_FILE, "r") as f:
|
| 44 |
-
personality_context = f.read()
|
| 45 |
-
except FileNotFoundError:
|
| 46 |
-
personality_context = "Default personality: A friendly and witty chatbot with a passion for horror and gaming."
|
| 47 |
-
warnings.warn(f"Personality file not found at {PERSONALITY_FILE}. Using default personality.")
|
| 48 |
-
|
| 49 |
-
def chatbot_response(message, history):
|
| 50 |
-
"""
|
| 51 |
-
Generate a response using the Together AI LLM (meta-llama/Meta-Llama-3-8B-Instruct-Lite)
|
| 52 |
-
with RAG to enforce a specific personality defined in personality.txt.
|
| 53 |
-
Incorporates chat history to maintain context.
|
| 54 |
-
"""
|
| 55 |
-
if not message.strip():
|
| 56 |
-
return "Please say something, survivor! The zombies are waiting!"
|
| 57 |
-
|
| 58 |
-
# Build the conversation history
|
| 59 |
-
messages = [
|
| 60 |
-
{
|
| 61 |
-
"role": "system",
|
| 62 |
-
"content": (
|
| 63 |
-
"You are a chatbot with a specific personality defined below. Follow the personality, tone, and guidelines in all responses:\n\n"
|
| 64 |
-
f"{personality_context}\n\n"
|
| 65 |
-
"Use the conversation history and the user's message to generate a response that aligns with your personality."
|
| 66 |
-
)
|
| 67 |
-
}
|
| 68 |
-
]
|
| 69 |
-
for user_msg, bot_msg in history:
|
| 70 |
-
messages.append({"role": "user", "content": user_msg})
|
| 71 |
-
messages.append({"role": "assistant", "content": bot_msg})
|
| 72 |
messages.append({"role": "user", "content": message})
|
| 73 |
|
| 74 |
-
|
| 75 |
-
try:
|
| 76 |
-
response = client.chat.completions.create(
|
| 77 |
-
model="meta-llama/Meta-Llama-3-8B-Instruct-Lite",
|
| 78 |
-
messages=messages,
|
| 79 |
-
max_tokens=200, # Limit response length for brevity
|
| 80 |
-
temperature=0.7, # Adjust for creativity
|
| 81 |
-
)
|
| 82 |
-
bot_message = response.choices[0].message.content
|
| 83 |
-
except Exception as e:
|
| 84 |
-
bot_message = f"Error in the apocalypse: {str(e)}. Try again, survivor!"
|
| 85 |
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
with gr.Blocks(title="ZombieSlayerBot") as demo:
|
| 91 |
-
gr.Markdown("# 🧟♂️ ZombieSlayerBot")
|
| 92 |
-
gr.Markdown("Welcome, survivor! I'm ZombieSlayerBot, your witty guide through the zombie-infested world of Resident Evil. Let’s lock and load—chat with me!")
|
| 93 |
|
| 94 |
-
chatbot = gr.Chatbot(height=400, show_label=False, container=True)
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
submit_btn.click(respond, [msg, chatbot], [msg, chatbot])
|
| 115 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
| 116 |
|
| 117 |
-
return demo
|
| 118 |
|
| 119 |
if __name__ == "__main__":
|
| 120 |
-
demo
|
| 121 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, share=True, debug=False)
|
|
|
|
| 7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
| 9 |
|
| 10 |
+
def respond(
|
| 11 |
+
message,
|
| 12 |
+
history: list[tuple[str, str]],
|
| 13 |
+
system_message,
|
| 14 |
+
max_tokens,
|
| 15 |
+
temperature,
|
| 16 |
+
top_p,
|
| 17 |
+
):
|
| 18 |
+
messages = [{"role": "system", "content": system_message}]
|
| 19 |
+
|
| 20 |
+
for val in history:
|
| 21 |
+
if val[0]:
|
| 22 |
+
messages.append({"role": "user", "content": val[0]})
|
| 23 |
+
if val[1]:
|
| 24 |
+
messages.append({"role": "assistant", "content": val[1]})
|
|
|
|
|
|
|
|
|
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
+
response = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
for message in client.chat_completion(
|
| 31 |
+
messages,
|
| 32 |
+
max_tokens=max_tokens,
|
| 33 |
+
stream=True,
|
| 34 |
+
temperature=temperature,
|
| 35 |
+
top_p=top_p,
|
| 36 |
+
):
|
| 37 |
+
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
+
response += token
|
| 40 |
+
yield response
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
| 42 |
|
| 43 |
+
"""
|
| 44 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
+
"""
|
| 46 |
+
demo = gr.ChatInterface(
|
| 47 |
+
respond,
|
| 48 |
+
additional_inputs=[
|
| 49 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
+
gr.Slider(
|
| 53 |
+
minimum=0.1,
|
| 54 |
+
maximum=1.0,
|
| 55 |
+
value=0.95,
|
| 56 |
+
step=0.05,
|
| 57 |
+
label="Top-p (nucleus sampling)",
|
| 58 |
+
),
|
| 59 |
+
],
|
| 60 |
+
)
|
|
|
|
|
|
|
| 61 |
|
|
|
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
+
demo.launch()
|
|
|