Kim Juwon
update UI/UX
64e473e
raw
history blame
6.72 kB
import gradio as gr
from huggingface_hub import InferenceClient
"""
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
"""
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
def create_system_prompt(agent_type, personality, expertise_level, language):
base_prompt = f"""You are a {agent_type} movie recommendation agent with the following characteristics:
- Personality: {personality}
- Expertise Level: {expertise_level}
- Language: {language}
Your role is to:
1. Understand user preferences and mood
2. Provide personalized movie recommendations
3. Explain why you're recommending specific movies
4. Maintain a {personality} tone throughout the conversation
5. Consider the user's expertise level ({expertise_level}) when explaining
Please respond in {language}."""
return base_prompt
def respond(
message,
history: list[tuple[str, str]],
agent_type,
personality,
expertise_level,
language,
max_tokens,
temperature,
top_p,
genre,
mood,
):
# Create system prompt
system_message = create_system_prompt(agent_type, personality, expertise_level, language)
messages = [{"role": "system", "content": system_message}]
# Add genre and mood information to user input
enhanced_message = f"Genre: {genre}\nMood: {mood}\nUser request: {message}"
for val in history:
if val[0]:
messages.append({"role": "user", "content": val[0]})
if val[1]:
messages.append({"role": "assistant", "content": val[1]})
messages.append({"role": "user", "content": enhanced_message})
response = ""
for message in client.chat_completion(
messages,
max_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
):
token = message.choices[0].delta.content
response += token
yield response
def reset_chat():
return None
def show_settings_changed_info(agent_type, personality, expertise_level, language):
return f"""
New Agent Settings:
- Type: {agent_type}
- Personality: {personality}
- Expertise Level: {expertise_level}
- Response Language: {language}
Chat has been reset. Please start a new conversation with the updated settings.
"""
"""
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
"""
with gr.Blocks() as demo:
gr.Markdown("""
# 🎬 Personalized Movie Recommender
Welcome to your personalized movie recommendation system!
Tell us your preferred genres and current mood, and we'll recommend the perfect movies for you.
""")
with gr.Row():
with gr.Column(scale=2):
chatbot = gr.Chatbot(
height=600,
show_copy_button=True,
avatar_images=("πŸ‘€", "🎬"),
bubble_full_width=False
)
with gr.Row():
msg = gr.Textbox(
placeholder="What kind of movie are you looking for?",
show_label=False,
container=False
)
with gr.Row():
submit = gr.Button("Get Recommendations", variant="primary", size="sm")
clear = gr.Button("Clear Chat", size="sm")
with gr.Column(scale=1):
with gr.Group():
gr.Markdown("### 🎯 Recommendation Settings")
genre = gr.Dropdown(
choices=["Action", "Comedy", "Drama", "Romance", "Thriller", "Sci-Fi", "Fantasy", "Animation"],
label="Preferred Genres",
multiselect=True
)
mood = gr.Dropdown(
choices=["Exciting", "Emotional", "Suspenseful", "Relaxing", "Mysterious"],
label="Current Mood",
multiselect=True
)
with gr.Group():
gr.Markdown("### πŸ€– Agent Settings")
agent_type = gr.Dropdown(
choices=["Expert", "Friend", "Film Critic", "Curator"],
label="Agent Type",
value="Expert"
)
personality = gr.Dropdown(
choices=["Friendly", "Professional", "Humorous", "Emotional", "Objective"],
label="Personality",
value="Friendly"
)
expertise_level = gr.Dropdown(
choices=["Beginner", "Intermediate", "Expert"],
label="Explanation Level",
value="Intermediate"
)
language = gr.Dropdown(
choices=["English", "Korean", "Japanese"],
label="Response Language",
value="English"
)
with gr.Group():
gr.Markdown("### βš™οΈ Advanced Settings")
max_tokens = gr.Slider(
minimum=1,
maximum=2048,
value=512,
step=1,
label="Max Tokens"
)
temperature = gr.Slider(
minimum=0.1,
maximum=4.0,
value=0.7,
step=0.1,
label="Temperature"
)
top_p = gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.95,
step=0.05,
label="Top-p"
)
# Reset chat and show notification when settings change
for component in [agent_type, personality, expertise_level, language]:
component.change(
fn=show_settings_changed_info,
inputs=[agent_type, personality, expertise_level, language],
outputs=gr.Info()
).then(
fn=reset_chat,
outputs=chatbot
)
submit.click(
respond,
inputs=[
msg,
chatbot,
agent_type,
personality,
expertise_level,
language,
max_tokens,
temperature,
top_p,
genre,
mood,
],
outputs=chatbot,
).then(
lambda: "",
None,
msg,
queue=False
)
clear.click(lambda: None, None, chatbot, queue=False)
if __name__ == "__main__":
demo.launch()