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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from src.llm import query_chroma
|
| 3 |
+
from src.reranker_warm import rank_anime_warm
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
import requests
|
| 7 |
+
|
| 8 |
+
css = """
|
| 9 |
+
footer {display: none !important}
|
| 10 |
+
.gradio-container {
|
| 11 |
+
max-width: 1200px;
|
| 12 |
+
margin: auto;
|
| 13 |
+
}
|
| 14 |
+
.contain {
|
| 15 |
+
background: rgba(255, 255, 255, 0.05);
|
| 16 |
+
border-radius: 12px;
|
| 17 |
+
padding: 20px;
|
| 18 |
+
}
|
| 19 |
+
.submit-btn {
|
| 20 |
+
background: linear-gradient(90deg, #4B79A1 0%, #283E51 100%) !important;
|
| 21 |
+
border: none !important;
|
| 22 |
+
color: white !important;
|
| 23 |
+
}
|
| 24 |
+
.submit-btn:hover {
|
| 25 |
+
transform: translateY(-2px);
|
| 26 |
+
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
|
| 27 |
+
}
|
| 28 |
+
.title {
|
| 29 |
+
text-align: center;
|
| 30 |
+
font-size: 2.5em;
|
| 31 |
+
font-weight: bold;
|
| 32 |
+
margin-bottom: 1em;
|
| 33 |
+
background: linear-gradient(90deg, #4B79A1 0%, #283E51 100%);
|
| 34 |
+
-webkit-background-clip: text;
|
| 35 |
+
-webkit-text-fill-color: transparent;
|
| 36 |
+
}
|
| 37 |
+
.output-image {
|
| 38 |
+
width: 100% !important;
|
| 39 |
+
max-width: 100% !important;
|
| 40 |
+
}
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def download_pic(names: list[str]):
|
| 45 |
+
df = pd.read_csv(str(Path(Path(__file__).parent, "src/data/final_anime_list.csv")))
|
| 46 |
+
pic_dir = Path(Path(__file__).parent, "src/data/pics")
|
| 47 |
+
|
| 48 |
+
if not pic_dir.exists():
|
| 49 |
+
pic_dir.mkdir(exist_ok=True)
|
| 50 |
+
|
| 51 |
+
df = df[df.Name.isin(names)]
|
| 52 |
+
df = df[["Name", "Image URL", "Synopsis"]].set_index("Name").reindex(names)
|
| 53 |
+
synopsis_list = df['Synopsis'].tolist()
|
| 54 |
+
file_paths = []
|
| 55 |
+
for url in df["Image URL"]:
|
| 56 |
+
file_name = "_".join(url.split("/")[-2:])
|
| 57 |
+
file_path = Path(pic_dir, file_name)
|
| 58 |
+
|
| 59 |
+
if file_path.exists():
|
| 60 |
+
file_paths.append(str(file_path))
|
| 61 |
+
continue
|
| 62 |
+
|
| 63 |
+
response = requests.get(url)
|
| 64 |
+
response.raise_for_status() # Raise an exception for bad status codes
|
| 65 |
+
|
| 66 |
+
with open(Path(pic_dir, file_name), 'wb') as file:
|
| 67 |
+
file.write(response.content)
|
| 68 |
+
file_paths.append(str(file_path))
|
| 69 |
+
|
| 70 |
+
print(f"Image downloaded successfully: {url}")
|
| 71 |
+
return file_paths, synopsis_list
|
| 72 |
+
# return url
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def integration_warm(query: str):
|
| 76 |
+
anime_name_list = query_chroma(query=query, anime_count=100)
|
| 77 |
+
|
| 78 |
+
# you need to have the following datasets in src/data/ to call this function
|
| 79 |
+
# 1. warm_rerank_data.csv
|
| 80 |
+
# 2. user_similarities.csv
|
| 81 |
+
# we saved the intermediate matrix for calculation efficiency
|
| 82 |
+
anime_name_list = rank_anime_warm(userid=12, anime_list=anime_name_list)[:4]
|
| 83 |
+
final_names = [x[0] for x in anime_name_list]
|
| 84 |
+
|
| 85 |
+
anime_pic_list, synopsis_list = download_pic(list(final_names))
|
| 86 |
+
|
| 87 |
+
return [*anime_name_list, *anime_pic_list, *synopsis_list]
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def clear_prompt():
|
| 93 |
+
"""Function to clear the prompt box."""
|
| 94 |
+
return ""
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def feedback_button(action, anime_name):
|
| 98 |
+
# Store or log feedback (can be expanded to save in a database)
|
| 99 |
+
return f"You {action}d {anime_name}!"
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 103 |
+
gr.HTML('<div class="title">AniQuest</div>')
|
| 104 |
+
gr.HTML(
|
| 105 |
+
'<div style="text-align: center; margin-bottom: 2em; color: #666; font-size: 24px;">We recommendate animes based on your description</div>')
|
| 106 |
+
gr.HTML("""
|
| 107 |
+
<div style="color: red; margin-bottom: 1em; text-align: center; padding: 10px; background: rgba(255,0,0,0.1); border-radius: 8px;">
|
| 108 |
+
โ ๏ธ Welcome, [user_id: 12] to this recommendation system โ ๏ธ
|
| 109 |
+
</div>
|
| 110 |
+
""")
|
| 111 |
+
|
| 112 |
+
with gr.Column():
|
| 113 |
+
prompt = gr.Textbox(
|
| 114 |
+
label="Query",
|
| 115 |
+
placeholder="Describe the anime you want to watch next ...",
|
| 116 |
+
lines=1
|
| 117 |
+
)
|
| 118 |
+
with gr.Row():
|
| 119 |
+
generate_btn = gr.Button(
|
| 120 |
+
"๐ Submit",
|
| 121 |
+
elem_classes=["submit-btn"]
|
| 122 |
+
)
|
| 123 |
+
clear_btn = gr.Button(
|
| 124 |
+
"๐
Clear",
|
| 125 |
+
elem_classes=["submit-btn"]
|
| 126 |
+
)
|
| 127 |
+
with gr.Row():
|
| 128 |
+
for i in range(4):
|
| 129 |
+
|
| 130 |
+
anime_names = [] # Store references to the anime name components
|
| 131 |
+
feedback_texts = [] # List to store feedback components
|
| 132 |
+
|
| 133 |
+
with gr.Column(scale=1, elem_classes=["anime-block"]):
|
| 134 |
+
|
| 135 |
+
exec(f"anime{i + 1} = gr.Textbox(label='Anime {i + 1}')")
|
| 136 |
+
|
| 137 |
+
with gr.Row(): # Add Like and Dislike buttons under each anime name
|
| 138 |
+
like_btn = gr.Button("๐ Like")
|
| 139 |
+
dislike_btn = gr.Button("๐ Dislike")
|
| 140 |
+
|
| 141 |
+
exec(f"image{i + 1} = gr.Image(label='Image', elem_classes=['output-image', 'fixed-width'])")
|
| 142 |
+
exec(
|
| 143 |
+
f"description{i + 1} = gr.HTML('<div class=\"anime-description\" style=\"margin-top: 10px; font-size: 14px; color: #666;\">Description for anime {i + 1}</div>')")
|
| 144 |
+
|
| 145 |
+
# anime_names.append(anime_name) # Store the reference to use in the button's click method
|
| 146 |
+
|
| 147 |
+
# feedback_text = gr.Textbox(
|
| 148 |
+
# label="Feedback",
|
| 149 |
+
# interactive=False,
|
| 150 |
+
# visible=False # Hidden initially; becomes visible after feedback
|
| 151 |
+
# )
|
| 152 |
+
# feedback_texts.append(feedback_text)
|
| 153 |
+
|
| 154 |
+
# Link Like and Dislike buttons to feedback function
|
| 155 |
+
# like_btn.click(fn=feedback_button, inputs=["Like", anime_name], outputs=feedback_text)
|
| 156 |
+
# dislike_btn.click(fn=feedback_button, inputs=["Dislike", anime_name], outputs=feedback_text)
|
| 157 |
+
|
| 158 |
+
generate_btn.click(
|
| 159 |
+
fn=integration_warm,
|
| 160 |
+
inputs=[prompt],
|
| 161 |
+
outputs=[anime1, anime2, anime3, anime4, image1, image2, image3, image4, description1, description2, description3, description4, ]
|
| 162 |
+
)
|
| 163 |
+
# Link the "Clear" button to the clear function to clear the prompt
|
| 164 |
+
clear_btn.click(
|
| 165 |
+
fn=clear_prompt, # The function to call
|
| 166 |
+
inputs=[], # No input required
|
| 167 |
+
outputs=[prompt] # Clears the prompt
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
if __name__ == "__main__":
|
| 174 |
+
# integration_warm(query='I want something like Demon Slayer, but with more romance and produced by Kyoto Animation')
|
| 175 |
+
# warm user
|
| 176 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|