Kim Juwon
commited on
Commit
Β·
39ade08
1
Parent(s):
a0491f0
update UI/UX
Browse files- __pycache__/app.cpython-39.pyc +0 -0
- app.py +93 -83
__pycache__/app.cpython-39.pyc
ADDED
|
Binary file (4.98 kB). View file
|
|
|
app.py
CHANGED
|
@@ -1,12 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
import json
|
| 4 |
|
| 5 |
-
MODAL_ENDPOINT = "https://kim-ju-won--
|
| 6 |
-
|
| 7 |
-
"""
|
| 8 |
-
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
|
| 9 |
-
"""
|
| 10 |
|
| 11 |
def create_system_prompt(agent_type, personality, expertise_level, language):
|
| 12 |
base_prompt = f"""You are a {agent_type} movie recommendation agent with the following characteristics:
|
|
@@ -24,57 +19,36 @@ Your role is to:
|
|
| 24 |
Please respond in {language}."""
|
| 25 |
return base_prompt
|
| 26 |
|
| 27 |
-
def respond(
|
| 28 |
-
message,
|
| 29 |
-
history: list[tuple[str, str]],
|
| 30 |
-
agent_type,
|
| 31 |
-
personality,
|
| 32 |
-
expertise_level,
|
| 33 |
-
language,
|
| 34 |
-
max_tokens,
|
| 35 |
-
temperature,
|
| 36 |
-
top_p,
|
| 37 |
-
genre,
|
| 38 |
-
mood,
|
| 39 |
-
):
|
| 40 |
-
# Create system prompt
|
| 41 |
system_message = create_system_prompt(agent_type, personality, expertise_level, language)
|
| 42 |
|
| 43 |
-
# Prepare messages for the API
|
| 44 |
messages = [{"role": "system", "content": system_message}]
|
| 45 |
|
| 46 |
-
# Add conversation history
|
| 47 |
for val in history:
|
| 48 |
if val[0]:
|
| 49 |
messages.append({"role": "user", "content": val[0]})
|
| 50 |
if val[1]:
|
| 51 |
messages.append({"role": "assistant", "content": val[1]})
|
| 52 |
|
| 53 |
-
# Add current message with genre and mood
|
| 54 |
enhanced_message = f"Genre: {genre}\nMood: {mood}\nUser request: {message}"
|
| 55 |
messages.append({"role": "user", "content": enhanced_message})
|
| 56 |
|
| 57 |
-
# Prepare request payload
|
| 58 |
payload = {
|
| 59 |
"messages": messages,
|
| 60 |
-
"max_tokens":
|
| 61 |
-
"temperature":
|
| 62 |
-
"top_p":
|
| 63 |
}
|
| 64 |
|
| 65 |
try:
|
| 66 |
-
# Send request to Modal endpoint
|
| 67 |
response = requests.post(
|
| 68 |
MODAL_ENDPOINT,
|
| 69 |
json=payload,
|
| 70 |
headers={"Content-Type": "application/json"}
|
| 71 |
)
|
| 72 |
response.raise_for_status()
|
| 73 |
-
|
| 74 |
-
# Get response from Modal
|
| 75 |
result = response.json()
|
| 76 |
return result.get("response", "Sorry, I couldn't process your request.")
|
| 77 |
-
|
| 78 |
except Exception as e:
|
| 79 |
return f"Error: {str(e)}"
|
| 80 |
|
|
@@ -92,15 +66,71 @@ def show_settings_changed_info(agent_type, personality, expertise_level, languag
|
|
| 92 |
Chat has been reset. Please start a new conversation with the updated settings.
|
| 93 |
"""
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
"""
|
| 98 |
|
| 99 |
-
with gr.Blocks() as demo:
|
| 100 |
gr.Markdown("""
|
| 101 |
# π¬ Personalized Movie Recommender
|
| 102 |
-
|
| 103 |
-
Welcome to your personalized movie recommendation system!
|
| 104 |
Tell us your preferred genres and current mood, and we'll recommend the perfect movies for you.
|
| 105 |
""")
|
| 106 |
|
|
@@ -120,70 +150,54 @@ with gr.Blocks() as demo:
|
|
| 120 |
min_width=400
|
| 121 |
)
|
| 122 |
submit = gr.Button("Send Chat", variant="primary", min_width=100)
|
| 123 |
-
clear = gr.Button("Clear Chat", min_width=100)
|
| 124 |
|
| 125 |
with gr.Column(scale=1):
|
| 126 |
with gr.Group():
|
| 127 |
gr.Markdown("### π― Recommendation Settings")
|
| 128 |
genre = gr.Dropdown(
|
| 129 |
-
choices=[
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
| 131 |
multiselect=True
|
| 132 |
)
|
| 133 |
mood = gr.Dropdown(
|
| 134 |
-
choices=[
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
| 136 |
multiselect=True
|
| 137 |
)
|
| 138 |
|
| 139 |
with gr.Group():
|
| 140 |
gr.Markdown("### π€ Agent Settings")
|
| 141 |
agent_type = gr.Dropdown(
|
| 142 |
-
choices=["Expert", "Friend", "Film Critic", "Curator"],
|
| 143 |
-
label="Agent Type",
|
| 144 |
-
value="Expert"
|
| 145 |
)
|
| 146 |
personality = gr.Dropdown(
|
| 147 |
-
choices=[
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
| 150 |
)
|
| 151 |
expertise_level = gr.Dropdown(
|
| 152 |
-
choices=["Beginner", "Intermediate", "Expert"],
|
| 153 |
-
label="Explanation Level",
|
| 154 |
-
value="Intermediate"
|
| 155 |
)
|
| 156 |
language = gr.Dropdown(
|
| 157 |
-
choices=["English", "Korean", "Japanese"],
|
| 158 |
-
label="Response Language",
|
| 159 |
-
value="English"
|
| 160 |
-
)
|
| 161 |
-
|
| 162 |
-
with gr.Group():
|
| 163 |
-
gr.Markdown("### βοΈ Advanced Settings")
|
| 164 |
-
max_tokens = gr.Slider(
|
| 165 |
-
minimum=1,
|
| 166 |
-
maximum=2048,
|
| 167 |
-
value=512,
|
| 168 |
-
step=1,
|
| 169 |
-
label="Max Tokens"
|
| 170 |
-
)
|
| 171 |
-
temperature = gr.Slider(
|
| 172 |
-
minimum=0.1,
|
| 173 |
-
maximum=4.0,
|
| 174 |
-
value=0.7,
|
| 175 |
-
step=0.1,
|
| 176 |
-
label="Temperature"
|
| 177 |
-
)
|
| 178 |
-
top_p = gr.Slider(
|
| 179 |
-
minimum=0.1,
|
| 180 |
-
maximum=1.0,
|
| 181 |
-
value=0.95,
|
| 182 |
-
step=0.05,
|
| 183 |
-
label="Top-p"
|
| 184 |
)
|
| 185 |
|
| 186 |
-
# Reset chat and show notification when settings change
|
| 187 |
for component in [agent_type, personality, expertise_level, language]:
|
| 188 |
component.change(
|
| 189 |
fn=show_settings_changed_info,
|
|
@@ -203,9 +217,6 @@ with gr.Blocks() as demo:
|
|
| 203 |
personality,
|
| 204 |
expertise_level,
|
| 205 |
language,
|
| 206 |
-
max_tokens,
|
| 207 |
-
temperature,
|
| 208 |
-
top_p,
|
| 209 |
genre,
|
| 210 |
mood,
|
| 211 |
],
|
|
@@ -219,6 +230,5 @@ with gr.Blocks() as demo:
|
|
| 219 |
|
| 220 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 221 |
|
| 222 |
-
|
| 223 |
if __name__ == "__main__":
|
| 224 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
+
MODAL_ENDPOINT = "https://kim-ju-won--gemma2b-chat-chat.modal.run"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def create_system_prompt(agent_type, personality, expertise_level, language):
|
| 7 |
base_prompt = f"""You are a {agent_type} movie recommendation agent with the following characteristics:
|
|
|
|
| 19 |
Please respond in {language}."""
|
| 20 |
return base_prompt
|
| 21 |
|
| 22 |
+
def respond(message, history, agent_type, personality, expertise_level, language, genre, mood):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
system_message = create_system_prompt(agent_type, personality, expertise_level, language)
|
| 24 |
|
|
|
|
| 25 |
messages = [{"role": "system", "content": system_message}]
|
| 26 |
|
|
|
|
| 27 |
for val in history:
|
| 28 |
if val[0]:
|
| 29 |
messages.append({"role": "user", "content": val[0]})
|
| 30 |
if val[1]:
|
| 31 |
messages.append({"role": "assistant", "content": val[1]})
|
| 32 |
|
|
|
|
| 33 |
enhanced_message = f"Genre: {genre}\nMood: {mood}\nUser request: {message}"
|
| 34 |
messages.append({"role": "user", "content": enhanced_message})
|
| 35 |
|
|
|
|
| 36 |
payload = {
|
| 37 |
"messages": messages,
|
| 38 |
+
"max_tokens": 512,
|
| 39 |
+
"temperature": 0.7,
|
| 40 |
+
"top_p": 0.95
|
| 41 |
}
|
| 42 |
|
| 43 |
try:
|
|
|
|
| 44 |
response = requests.post(
|
| 45 |
MODAL_ENDPOINT,
|
| 46 |
json=payload,
|
| 47 |
headers={"Content-Type": "application/json"}
|
| 48 |
)
|
| 49 |
response.raise_for_status()
|
|
|
|
|
|
|
| 50 |
result = response.json()
|
| 51 |
return result.get("response", "Sorry, I couldn't process your request.")
|
|
|
|
| 52 |
except Exception as e:
|
| 53 |
return f"Error: {str(e)}"
|
| 54 |
|
|
|
|
| 66 |
Chat has been reset. Please start a new conversation with the updated settings.
|
| 67 |
"""
|
| 68 |
|
| 69 |
+
# π μλ²½ν λ€ν¬/λΌμ΄νΈ λͺ¨λ CSS
|
| 70 |
+
custom_css = """
|
| 71 |
+
/* κΈ°λ³Έ λΌμ΄νΈ λͺ¨λ */
|
| 72 |
+
body, .gradio-container {
|
| 73 |
+
background-color: #f9f9fb;
|
| 74 |
+
color: #333;
|
| 75 |
+
}
|
| 76 |
+
.gr-box, .gr-group, .gr-column, .gr-dropdown, .gr-textbox, .gr-markdown {
|
| 77 |
+
background-color: #ffffff;
|
| 78 |
+
color: #333;
|
| 79 |
+
border: 1px solid #e0e0e0;
|
| 80 |
+
border-radius: 8px;
|
| 81 |
+
box-shadow: 1px 1px 3px rgba(0,0,0,0.05);
|
| 82 |
+
}
|
| 83 |
+
.gr-chatbot {
|
| 84 |
+
background-color: #f0f0f5;
|
| 85 |
+
color: #333;
|
| 86 |
+
border-radius: 10px;
|
| 87 |
+
}
|
| 88 |
+
.gr-button-primary {
|
| 89 |
+
background-color: #a78bfa !important;
|
| 90 |
+
color: #fff !important;
|
| 91 |
+
}
|
| 92 |
+
.gr-button-secondary {
|
| 93 |
+
background-color: #d1c4e9 !important;
|
| 94 |
+
color: #333 !important;
|
| 95 |
+
}
|
| 96 |
+
.gr-dropdown select, .gr-textbox textarea {
|
| 97 |
+
background-color: #ffffff;
|
| 98 |
+
color: #333;
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
/* λ€ν¬ λͺ¨λ */
|
| 102 |
+
@media (prefers-color-scheme: dark) {
|
| 103 |
+
body, .gradio-container {
|
| 104 |
+
background-color: #121212;
|
| 105 |
+
color: #e0e0e0;
|
| 106 |
+
}
|
| 107 |
+
.gr-box, .gr-group, .gr-column, .gr-dropdown, .gr-textbox, .gr-markdown {
|
| 108 |
+
background-color: #1e1e1e;
|
| 109 |
+
color: #e0e0e0;
|
| 110 |
+
border: 1px solid #333;
|
| 111 |
+
}
|
| 112 |
+
.gr-chatbot {
|
| 113 |
+
background-color: #1a1a1a;
|
| 114 |
+
color: #e0e0e0;
|
| 115 |
+
}
|
| 116 |
+
.gr-button-primary {
|
| 117 |
+
background-color: #ff7847 !important;
|
| 118 |
+
color: #fff !important;
|
| 119 |
+
}
|
| 120 |
+
.gr-button-secondary {
|
| 121 |
+
background-color: #555 !important;
|
| 122 |
+
color: #fff !important;
|
| 123 |
+
}
|
| 124 |
+
.gr-dropdown select, .gr-textbox textarea {
|
| 125 |
+
background-color: #1e1e1e;
|
| 126 |
+
color: #e0e0e0;
|
| 127 |
+
}
|
| 128 |
+
}
|
| 129 |
"""
|
| 130 |
|
| 131 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 132 |
gr.Markdown("""
|
| 133 |
# π¬ Personalized Movie Recommender
|
|
|
|
|
|
|
| 134 |
Tell us your preferred genres and current mood, and we'll recommend the perfect movies for you.
|
| 135 |
""")
|
| 136 |
|
|
|
|
| 150 |
min_width=400
|
| 151 |
)
|
| 152 |
submit = gr.Button("Send Chat", variant="primary", min_width=100)
|
| 153 |
+
clear = gr.Button("Clear Chat", variant="secondary", min_width=100)
|
| 154 |
|
| 155 |
with gr.Column(scale=1):
|
| 156 |
with gr.Group():
|
| 157 |
gr.Markdown("### π― Recommendation Settings")
|
| 158 |
genre = gr.Dropdown(
|
| 159 |
+
choices=[
|
| 160 |
+
"π¬ Action", "π Comedy", "π Drama", "π Romance",
|
| 161 |
+
"πͺ Thriller", "π½ Sci-Fi", "π§ Fantasy", "π¨ Animation"
|
| 162 |
+
],
|
| 163 |
+
label="Preferred Genres π₯",
|
| 164 |
multiselect=True
|
| 165 |
)
|
| 166 |
mood = gr.Dropdown(
|
| 167 |
+
choices=[
|
| 168 |
+
"β‘ Exciting", "π Emotional", "π± Suspenseful",
|
| 169 |
+
"π Relaxing", "π΅οΈ Mysterious"
|
| 170 |
+
],
|
| 171 |
+
label="Current Mood π",
|
| 172 |
multiselect=True
|
| 173 |
)
|
| 174 |
|
| 175 |
with gr.Group():
|
| 176 |
gr.Markdown("### π€ Agent Settings")
|
| 177 |
agent_type = gr.Dropdown(
|
| 178 |
+
choices=["π Expert", "π― Friend", "π₯ Film Critic", "π¨ Curator"],
|
| 179 |
+
label="Agent Type π§βπΌ",
|
| 180 |
+
value="π Expert"
|
| 181 |
)
|
| 182 |
personality = gr.Dropdown(
|
| 183 |
+
choices=[
|
| 184 |
+
"π Friendly", "πΌ Professional", "π Humorous",
|
| 185 |
+
"π₯Ί Emotional", "π Objective"
|
| 186 |
+
],
|
| 187 |
+
label="Personality π«",
|
| 188 |
+
value="π Friendly"
|
| 189 |
)
|
| 190 |
expertise_level = gr.Dropdown(
|
| 191 |
+
choices=["πΌ Beginner", "π Intermediate", "π Expert"],
|
| 192 |
+
label="Explanation Level π",
|
| 193 |
+
value="π Intermediate"
|
| 194 |
)
|
| 195 |
language = gr.Dropdown(
|
| 196 |
+
choices=["π¬π§ English", "π°π· Korean", "π―π΅ Japanese"],
|
| 197 |
+
label="Response Language π",
|
| 198 |
+
value="π¬π§ English"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
)
|
| 200 |
|
|
|
|
| 201 |
for component in [agent_type, personality, expertise_level, language]:
|
| 202 |
component.change(
|
| 203 |
fn=show_settings_changed_info,
|
|
|
|
| 217 |
personality,
|
| 218 |
expertise_level,
|
| 219 |
language,
|
|
|
|
|
|
|
|
|
|
| 220 |
genre,
|
| 221 |
mood,
|
| 222 |
],
|
|
|
|
| 230 |
|
| 231 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 232 |
|
|
|
|
| 233 |
if __name__ == "__main__":
|
| 234 |
+
demo.launch()
|