Spaces:
Running
Running
Kaung Myat Htet
commited on
Commit
·
2c9b5e9
1
Parent(s):
34e1933
fix the prompt error
Browse files
app.py
CHANGED
@@ -5,7 +5,7 @@ import pandas as pd
|
|
5 |
from openai import OpenAI
|
6 |
import gradio as gr
|
7 |
|
8 |
-
input_file = "
|
9 |
user_df = pd.read_json(input_file, lines=True)
|
10 |
|
11 |
user_ids = user_df["user_id"].unique().tolist()
|
@@ -61,13 +61,13 @@ def get_books(user_id):
|
|
61 |
max_tokens=150
|
62 |
).choices[0].message.content.strip()
|
63 |
profile_cache[cache_key] = profile_response
|
64 |
-
candidates_options = user_info.get("candidate_options", [])
|
65 |
rec_prompt = build_recommendation_prompt(profile_response, candidates_options)
|
66 |
choice = extract_choice(rec_prompt)
|
67 |
-
predicted_book = candidates_options
|
68 |
target_book = user_info.get("target_asin", '')
|
69 |
print("target_book:", target_book)
|
70 |
-
return f"{user_id}", df, guidance_response, profile_response, rec_prompt, pd.DataFrame(candidates_options
|
71 |
|
72 |
def extract_choice(response_text):
|
73 |
for token in response_text.split():
|
@@ -90,8 +90,10 @@ def profile_prompt(titles, guidance):
|
|
90 |
|
91 |
def build_recommendation_prompt(profile, candidates):
|
92 |
prompt = f"""A user has the following reading preference:\n"{profile}"\n\nHere are some books they might consider next:\n"""
|
|
|
|
|
93 |
for i, book in enumerate(candidates, start=1):
|
94 |
-
prompt += f"[{i}] {book
|
95 |
prompt += "\nWhich of these books best matches the user's preference? Respond ONLY with the number [1-4]."
|
96 |
return prompt
|
97 |
|
@@ -111,7 +113,7 @@ with gr.Blocks() as demo:
|
|
111 |
output_theme = gr.Textbox(label="User Books Theme", lines=8, show_label=False)
|
112 |
gr.Markdown("## User Profile")
|
113 |
output_profile = gr.Textbox(label="User Profile", show_label=False, lines=6)
|
114 |
-
output_rec_prompt = gr.Textbox(label="Recommendation Prompt")
|
115 |
output_candidate_options = gr.DataFrame(label="Candidate Books")
|
116 |
output_target_id = gr.Textbox(label="Target Book")
|
117 |
output_predicted_book = gr.Textbox(label="Predicted Book")
|
|
|
5 |
from openai import OpenAI
|
6 |
import gradio as gr
|
7 |
|
8 |
+
input_file = "./data/sample_gpg_data.jsonl"
|
9 |
user_df = pd.read_json(input_file, lines=True)
|
10 |
|
11 |
user_ids = user_df["user_id"].unique().tolist()
|
|
|
61 |
max_tokens=150
|
62 |
).choices[0].message.content.strip()
|
63 |
profile_cache[cache_key] = profile_response
|
64 |
+
candidates_options = list(user_info.get("candidate_options", []))
|
65 |
rec_prompt = build_recommendation_prompt(profile_response, candidates_options)
|
66 |
choice = extract_choice(rec_prompt)
|
67 |
+
predicted_book = candidates_options[choice-1] if choice and 1 <= choice <= len(candidates_options) else None
|
68 |
target_book = user_info.get("target_asin", '')
|
69 |
print("target_book:", target_book)
|
70 |
+
return f"{user_id}", df, guidance_response, profile_response, rec_prompt, pd.DataFrame(candidates_options[0]), target_book.values, predicted_book[0]['asin']
|
71 |
|
72 |
def extract_choice(response_text):
|
73 |
for token in response_text.split():
|
|
|
90 |
|
91 |
def build_recommendation_prompt(profile, candidates):
|
92 |
prompt = f"""A user has the following reading preference:\n"{profile}"\n\nHere are some books they might consider next:\n"""
|
93 |
+
if len(candidates) == 1 and isinstance(candidates[0], list):
|
94 |
+
candidates = candidates[0]
|
95 |
for i, book in enumerate(candidates, start=1):
|
96 |
+
prompt += f"[{i}] {book.get('title', 'Unknown Title')}\n"
|
97 |
prompt += "\nWhich of these books best matches the user's preference? Respond ONLY with the number [1-4]."
|
98 |
return prompt
|
99 |
|
|
|
113 |
output_theme = gr.Textbox(label="User Books Theme", lines=8, show_label=False)
|
114 |
gr.Markdown("## User Profile")
|
115 |
output_profile = gr.Textbox(label="User Profile", show_label=False, lines=6)
|
116 |
+
output_rec_prompt = gr.Textbox(label="Recommendation Prompt", lines=8)
|
117 |
output_candidate_options = gr.DataFrame(label="Candidate Books")
|
118 |
output_target_id = gr.Textbox(label="Target Book")
|
119 |
output_predicted_book = gr.Textbox(label="Predicted Book")
|