Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +34 -15
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -8,6 +8,7 @@ import openai
|
|
| 8 |
from openai import OpenAI
|
| 9 |
import gradio as gr
|
| 10 |
import huggingface_hub
|
|
|
|
| 11 |
|
| 12 |
api_key = os.environ.get("API_TOKEN")
|
| 13 |
headers = {
|
|
@@ -15,17 +16,8 @@ headers = {
|
|
| 15 |
'Content-Type': 'application/json'
|
| 16 |
}
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
DATA_FILE = os.path.join("data", DATA_FILENAME)
|
| 21 |
-
|
| 22 |
-
# HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 23 |
-
|
| 24 |
-
# repo = Repository(
|
| 25 |
-
# local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
|
| 26 |
-
# )
|
| 27 |
-
|
| 28 |
-
user_agents = pd.read_csv('./user_agents.csv')
|
| 29 |
user_agents = user_agents.iloc[:,1:]
|
| 30 |
user_batch = user_agents[:10]
|
| 31 |
|
|
@@ -91,14 +83,41 @@ def query_agent(description, question, image0, image1):
|
|
| 91 |
print(f"Failed to analyze {image0} and {image1} after 3 attempts.")
|
| 92 |
return None, None
|
| 93 |
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
demo = gr.Interface(fn=
|
| 97 |
-
inputs=[
|
|
|
|
|
|
|
| 98 |
gr.UploadButton("Click to Upload Email 1", file_types=["image"], file_count="1")],
|
| 99 |
outputs="text",
|
| 100 |
title="Pairwise Simulation of Petco Email Preference",
|
| 101 |
-
description=
|
| 102 |
)
|
| 103 |
|
| 104 |
if __name__ == "__main__":
|
|
|
|
| 8 |
from openai import OpenAI
|
| 9 |
import gradio as gr
|
| 10 |
import huggingface_hub
|
| 11 |
+
from datasets import load_dataset
|
| 12 |
|
| 13 |
api_key = os.environ.get("API_TOKEN")
|
| 14 |
headers = {
|
|
|
|
| 16 |
'Content-Type': 'application/json'
|
| 17 |
}
|
| 18 |
|
| 19 |
+
dataset = load_dataset('csv', data_files='https://huggingface.co/datasets/petcoblue/simulation_data/resolve/main/user_agents.csv')
|
| 20 |
+
user_agents = dataset.to_pandas()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
user_agents = user_agents.iloc[:,1:]
|
| 22 |
user_batch = user_agents[:10]
|
| 23 |
|
|
|
|
| 83 |
print(f"Failed to analyze {image0} and {image1} after 3 attempts.")
|
| 84 |
return None, None
|
| 85 |
|
| 86 |
+
def simulate(user_batch, question, image0, image1):
|
| 87 |
+
preferences = []
|
| 88 |
+
reasons = []
|
| 89 |
+
probs = []
|
| 90 |
+
for index, user_agent in user_batch.iterrows():
|
| 91 |
+
description = create_description(user_agent)
|
| 92 |
+
preference, top_logprobs = query_agent(description, question, image0, image1)
|
| 93 |
+
prob = np.round(np.exp(top_logprobs[0]['logprob']) * 100, 2)
|
| 94 |
+
|
| 95 |
+
split = preference.split("; Characteristics: ")
|
| 96 |
+
if len(split) == 2:
|
| 97 |
+
choice, reasoning = split[0], split[1]
|
| 98 |
+
else:
|
| 99 |
+
print(preference)
|
| 100 |
+
choice, reasoning = split[0], ""
|
| 101 |
+
|
| 102 |
+
preferences.append(0 if "0" in choice else 1)
|
| 103 |
+
reasons.append(reasoning)
|
| 104 |
+
probs.append(prob)
|
| 105 |
+
|
| 106 |
+
avg_preference = sum(preferences) / len(preferences)
|
| 107 |
+
avg_prob = sum(probs) / len(preferences)
|
| 108 |
+
|
| 109 |
+
return avg_preference
|
| 110 |
+
|
| 111 |
+
subtitle = "Upload two images of emails and see which is generally preferred by Petco customers!"
|
| 112 |
|
| 113 |
+
demo = gr.Interface(fn=simulate,
|
| 114 |
+
inputs=[user_batch,
|
| 115 |
+
question,
|
| 116 |
+
gr.UploadButton("Click to Upload Email 0", file_types=["image"], file_count="1"),
|
| 117 |
gr.UploadButton("Click to Upload Email 1", file_types=["image"], file_count="1")],
|
| 118 |
outputs="text",
|
| 119 |
title="Pairwise Simulation of Petco Email Preference",
|
| 120 |
+
description=subtitle
|
| 121 |
)
|
| 122 |
|
| 123 |
if __name__ == "__main__":
|
requirements.txt
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
openai
|
| 2 |
https://gradio-builds.s3.amazonaws.com/75ed61524f55e542414d165ed60a976091c24f77/gradio-3.16.2-py3-none-any.whl
|
| 3 |
pandas
|
| 4 |
-
numpy
|
|
|
|
|
|
| 1 |
openai
|
| 2 |
https://gradio-builds.s3.amazonaws.com/75ed61524f55e542414d165ed60a976091c24f77/gradio-3.16.2-py3-none-any.whl
|
| 3 |
pandas
|
| 4 |
+
numpy
|
| 5 |
+
datasets
|