Spaces:
Sleeping
Sleeping
record result update
Browse files- app.py +43 -74
- requirements.txt +2 -1
app.py
CHANGED
@@ -6,28 +6,42 @@ import json_repair
|
|
6 |
import random
|
7 |
import os
|
8 |
import re
|
|
|
|
|
9 |
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
10 |
|
11 |
-
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
task_completed_steps = None
|
15 |
|
16 |
|
17 |
-
def
|
18 |
-
return example["task_completed"] is None and example["task_completed_steps"] is None
|
19 |
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
23 |
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
|
26 |
-
return example["task_completed"] is None and example["task_completed_steps"] is None
|
27 |
|
28 |
|
29 |
-
def
|
30 |
-
|
|
|
|
|
|
|
|
|
31 |
|
32 |
|
33 |
class AppSimulator:
|
@@ -35,6 +49,7 @@ class AppSimulator:
|
|
35 |
self.simulation = None
|
36 |
self.openai_api_key = openai_api_key
|
37 |
self.app_name = None
|
|
|
38 |
|
39 |
def initialize_simulator(self, sitemap_url, progress=gr.Progress(track_tqdm=True)):
|
40 |
"""Initialize the simulator with retries and elapsed time tracking."""
|
@@ -53,23 +68,16 @@ class AppSimulator:
|
|
53 |
system_data = json_repair.loads(row['value'])
|
54 |
|
55 |
self.app_name = app_name
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
incomplete_tasks = synthetic_tasks.filter(is_task_incomplete)
|
60 |
-
give_up_tasks = synthetic_tasks.filter(is_task_given_up)
|
61 |
-
if len(incomplete_tasks) > 0:
|
62 |
-
incomplete_task = incomplete_tasks[0]
|
63 |
-
task = incomplete_task["tasks"]
|
64 |
-
solution = incomplete_task["steps"]
|
65 |
-
user_data = incomplete_task["attributes"]["user_data"]
|
66 |
-
elif len(give_up_tasks) > 0:
|
67 |
-
give_up_task = give_up_tasks[0]
|
68 |
-
task = give_up_task["tasks"]
|
69 |
-
solution = give_up_task["steps"]
|
70 |
-
user_data = give_up_task["attributes"]["user_data"]
|
71 |
-
else:
|
72 |
return "All tasks in this app have been completed!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
self.simulation = ChatbotSimulation(
|
75 |
app_name=app_name,
|
@@ -108,66 +116,27 @@ def chat(user_input, history):
|
|
108 |
pattern = r"Task completed! You took (\d+) steps\."
|
109 |
match = re.match(pattern, response)
|
110 |
|
111 |
-
global task_completed
|
112 |
-
global task_completed_steps
|
113 |
-
task_completed = 0
|
114 |
-
task_completed_steps = None
|
115 |
if match:
|
116 |
-
|
117 |
-
|
118 |
-
human_result_url = "jjz5463/simulator_human_result"
|
119 |
app_name = simulator_app.app_name
|
120 |
-
|
121 |
-
|
122 |
-
if len(incomplete_tasks) > 0:
|
123 |
-
incomplete_task_index = incomplete_tasks['"__index"'][0]
|
124 |
-
synthetic_tasks = synthetic_tasks.map(
|
125 |
-
lambda example, idx: {
|
126 |
-
"task_completed": task_completed if idx == incomplete_task_index else example["task_completed"],
|
127 |
-
"task_completed_steps": task_completed_steps if idx == incomplete_task_index else example["task_completed_steps"],
|
128 |
-
},
|
129 |
-
with_indices=True,
|
130 |
-
)
|
131 |
-
# Push the updated dataset back to Hugging Face
|
132 |
-
synthetic_tasks.push_to_hub(human_result_url)
|
133 |
|
134 |
return response
|
135 |
|
136 |
|
137 |
def give_up():
|
138 |
"""Handle the Give-Up action by marking the first incomplete task as abandoned."""
|
139 |
-
global task_completed, task_completed_steps
|
140 |
task_completed = 0
|
141 |
task_completed_steps = 0
|
142 |
|
143 |
-
# Access the app_name from the simulator instance
|
144 |
app_name = simulator_app.app_name
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
# Load the human result dataset
|
149 |
-
human_result_url = "jjz5463/simulator_human_result"
|
150 |
-
synthetic_tasks = load_dataset(human_result_url, app_name, split='train')
|
151 |
-
|
152 |
-
# Find the first incomplete task
|
153 |
-
incomplete_tasks = synthetic_tasks.filter(is_task_incomplete_idx, with_indices=True,)
|
154 |
-
if len(incomplete_tasks) > 0:
|
155 |
-
incomplete_task_index = incomplete_tasks['"__index"'][0]
|
156 |
-
|
157 |
-
# Update the dataset to mark the task as abandoned
|
158 |
-
synthetic_tasks = synthetic_tasks.map(
|
159 |
-
lambda example, idx: {
|
160 |
-
"task_completed": task_completed if idx == incomplete_task_index else example["task_completed"],
|
161 |
-
"steps": task_completed_steps if idx == incomplete_task_index else example["steps"],
|
162 |
-
},
|
163 |
-
with_indices=True,
|
164 |
-
)
|
165 |
|
166 |
-
|
167 |
-
synthetic_tasks.push_to_hub(human_result_url)
|
168 |
-
return "Task marked as abandoned (Give-Up action)."
|
169 |
-
else:
|
170 |
-
return "No incomplete tasks found to abandon!"
|
171 |
|
172 |
|
173 |
# Gradio Interface using ChatInterface
|
|
|
6 |
import random
|
7 |
import os
|
8 |
import re
|
9 |
+
import firebase_admin
|
10 |
+
from firebase_admin import credentials, firestore
|
11 |
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
12 |
|
13 |
+
cred = credentials.Certificate(os.getenv("Firebase_JSON"))
|
14 |
+
firebase_admin.initialize_app(cred)
|
15 |
+
db = firestore.client()
|
16 |
|
17 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
|
|
18 |
|
19 |
|
20 |
+
def find_smallest_incomplete_task(app_name):
|
|
|
21 |
|
22 |
+
collection_ref = db.collection(app_name)
|
23 |
+
docs = collection_ref.stream()
|
24 |
|
25 |
+
smallest_incomplete_idx = None
|
26 |
+
for doc in docs:
|
27 |
+
doc_id = doc.id
|
28 |
+
_, idx = doc_id.split('_')
|
29 |
+
idx = int(idx)
|
30 |
|
31 |
+
task_data = doc.to_dict()
|
32 |
+
if task_data['task_completed'] is None and task_data['task_completed_steps'] is None:
|
33 |
+
if smallest_incomplete_idx is None or idx < smallest_incomplete_idx:
|
34 |
+
smallest_incomplete_idx = idx
|
35 |
|
36 |
+
return smallest_incomplete_idx
|
|
|
37 |
|
38 |
|
39 |
+
def write_task_data(app_name, idx, task_complete, task_completed_step):
|
40 |
+
doc_ref = db.collection(app_name).document(f"{app_name}_{idx}")
|
41 |
+
doc_ref.set({
|
42 |
+
"task_completed": task_complete,
|
43 |
+
"task_completed_steps": task_completed_step
|
44 |
+
})
|
45 |
|
46 |
|
47 |
class AppSimulator:
|
|
|
49 |
self.simulation = None
|
50 |
self.openai_api_key = openai_api_key
|
51 |
self.app_name = None
|
52 |
+
self.smallest_index = None
|
53 |
|
54 |
def initialize_simulator(self, sitemap_url, progress=gr.Progress(track_tqdm=True)):
|
55 |
"""Initialize the simulator with retries and elapsed time tracking."""
|
|
|
68 |
system_data = json_repair.loads(row['value'])
|
69 |
|
70 |
self.app_name = app_name
|
71 |
+
smallest_index = find_smallest_incomplete_task(app_name)
|
72 |
+
if smallest_index is None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
return "All tasks in this app have been completed!"
|
74 |
+
self.smallest_index = smallest_index
|
75 |
+
|
76 |
+
synthetic_tasks = load_dataset(sitemap_url, "tasks", split='train')
|
77 |
+
incomplete_task = synthetic_tasks[smallest_index]
|
78 |
+
task = incomplete_task["tasks"]
|
79 |
+
solution = incomplete_task["steps"]
|
80 |
+
user_data = incomplete_task["attributes"]["user_data"]
|
81 |
|
82 |
self.simulation = ChatbotSimulation(
|
83 |
app_name=app_name,
|
|
|
116 |
pattern = r"Task completed! You took (\d+) steps\."
|
117 |
match = re.match(pattern, response)
|
118 |
|
|
|
|
|
|
|
|
|
119 |
if match:
|
120 |
+
task_complete = 1
|
121 |
+
task_completed_step = int(match.group(1))
|
|
|
122 |
app_name = simulator_app.app_name
|
123 |
+
idx = simulator_app.smallest_index
|
124 |
+
write_task_data(app_name, idx, task_complete, task_completed_step)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
|
126 |
return response
|
127 |
|
128 |
|
129 |
def give_up():
|
130 |
"""Handle the Give-Up action by marking the first incomplete task as abandoned."""
|
|
|
131 |
task_completed = 0
|
132 |
task_completed_steps = 0
|
133 |
|
|
|
134 |
app_name = simulator_app.app_name
|
135 |
+
idx = simulator_app.smallest_index
|
136 |
+
|
137 |
+
write_task_data(app_name, idx, task_completed, task_completed_steps)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
|
139 |
+
return "Task marked as abandoned (Give-Up action)."
|
|
|
|
|
|
|
|
|
140 |
|
141 |
|
142 |
# Gradio Interface using ChatInterface
|
requirements.txt
CHANGED
@@ -1,3 +1,4 @@
|
|
1 |
datadreamer.dev==0.38.0
|
2 |
huggingface-hub==0.24.7
|
3 |
-
json_repair
|
|
|
|
1 |
datadreamer.dev==0.38.0
|
2 |
huggingface-hub==0.24.7
|
3 |
+
json_repair
|
4 |
+
firebase_admin
|