Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -26,12 +26,9 @@ def process_event(event):
|
|
26 |
base_url="https://api.deepinfra.com/v1/openai",
|
27 |
)
|
28 |
|
29 |
-
|
30 |
llm_prompt = f"""
|
31 |
-
|
32 |
-
|
33 |
-
Dont make Any Assumptions,if value doesnt exist,Consider it as Zero.
|
34 |
-
|
35 |
{{
|
36 |
"market": "str",
|
37 |
"total_spend": "float",
|
@@ -52,20 +49,27 @@ def process_event(event):
|
|
52 |
"revenue_total_revenue": "float",
|
53 |
"revenue_roi": "float"
|
54 |
}}
|
55 |
-
|
56 |
-
Here is Text for it:
|
57 |
-
|
58 |
{event}
|
59 |
-
|
60 |
-
|
61 |
-
Return in only JSON adhering to Above Schema
|
62 |
"""
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
def process_all_events(events):
|
71 |
json_all = []
|
@@ -77,12 +81,10 @@ def process_all_events(events):
|
|
77 |
for i, future in enumerate(concurrent.futures.as_completed(futures)):
|
78 |
progress = (i + 1) / len(events)
|
79 |
progress_bar.progress(progress)
|
80 |
-
# st.write(f"Processed event {i + 1}/{len(events)}")
|
81 |
json_all.append(future.result())
|
82 |
|
83 |
return json_all
|
84 |
|
85 |
-
|
86 |
def main():
|
87 |
st.title("Rod Wave Concert Marketing Data Processor")
|
88 |
|
@@ -100,7 +102,7 @@ def main():
|
|
100 |
events = re.split(r'\n(?=Rod Wave Concert)', text)
|
101 |
events = [event for event in events if event.strip()]
|
102 |
|
103 |
-
st.write(f"Found {len(events)} events to process")
|
104 |
|
105 |
if st.button("Process Data"):
|
106 |
with st.spinner("Processing events..."):
|
@@ -108,7 +110,10 @@ def main():
|
|
108 |
|
109 |
json_sanity = []
|
110 |
for ele in json_all:
|
111 |
-
|
|
|
|
|
|
|
112 |
|
113 |
df = pd.DataFrame(json_sanity)
|
114 |
|
|
|
26 |
base_url="https://api.deepinfra.com/v1/openai",
|
27 |
)
|
28 |
|
|
|
29 |
llm_prompt = f"""
|
30 |
+
You are a digital marketing campaign analyst designed to analyze and report digital marketing campaign data for Rod Wave concerts. Your job is to convert the given text into JSON.
|
31 |
+
Don't make any assumptions; if a value doesn't exist, consider it as zero.
|
|
|
|
|
32 |
{{
|
33 |
"market": "str",
|
34 |
"total_spend": "float",
|
|
|
49 |
"revenue_total_revenue": "float",
|
50 |
"revenue_roi": "float"
|
51 |
}}
|
52 |
+
Here is the text for it:
|
|
|
|
|
53 |
{event}
|
54 |
+
Return in only JSON adhering to the above schema.
|
|
|
|
|
55 |
"""
|
56 |
|
57 |
+
# Attempt to process the event and validate JSON
|
58 |
+
for attempt in range(2): # Try twice
|
59 |
+
chat_completion = openai.chat.completions.create(
|
60 |
+
model="Qwen/Qwen2.5-Coder-32B-Instruct",
|
61 |
+
messages=[{"role": "user", "content": llm_prompt}],
|
62 |
+
)
|
63 |
+
json_output = chat_completion.choices[0].message.content
|
64 |
+
|
65 |
+
try:
|
66 |
+
return extract_and_parse_json_from_markdown(json_output)
|
67 |
+
except ValueError:
|
68 |
+
if attempt == 0:
|
69 |
+
st.warning("JSON validation failed, retrying...")
|
70 |
+
else:
|
71 |
+
st.error("Failed to validate JSON after retrying.")
|
72 |
+
return None # Return None if it fails after retrying
|
73 |
|
74 |
def process_all_events(events):
|
75 |
json_all = []
|
|
|
81 |
for i, future in enumerate(concurrent.futures.as_completed(futures)):
|
82 |
progress = (i + 1) / len(events)
|
83 |
progress_bar.progress(progress)
|
|
|
84 |
json_all.append(future.result())
|
85 |
|
86 |
return json_all
|
87 |
|
|
|
88 |
def main():
|
89 |
st.title("Rod Wave Concert Marketing Data Processor")
|
90 |
|
|
|
102 |
events = re.split(r'\n(?=Rod Wave Concert)', text)
|
103 |
events = [event for event in events if event.strip()]
|
104 |
|
105 |
+
st.write(f"Found **{len(events)}** events to process")
|
106 |
|
107 |
if st.button("Process Data"):
|
108 |
with st.spinner("Processing events..."):
|
|
|
110 |
|
111 |
json_sanity = []
|
112 |
for ele in json_all:
|
113 |
+
if ele is not None: # Only process valid JSON
|
114 |
+
json_sanity.append(ele)
|
115 |
+
st.subheader("Processed JSON for Event")
|
116 |
+
st.json(ele)
|
117 |
|
118 |
df = pd.DataFrame(json_sanity)
|
119 |
|