Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import re
|
4 |
+
from openai import OpenAI
|
5 |
+
import concurrent.futures
|
6 |
+
import json
|
7 |
+
import os
|
8 |
+
|
9 |
+
def extract_and_parse_json_from_markdown(markdown_text: str) -> dict:
|
10 |
+
code_block_pattern = r"``````"
|
11 |
+
code_block_match = re.search(code_block_pattern, markdown_text)
|
12 |
+
|
13 |
+
if code_block_match:
|
14 |
+
json_str = code_block_match.group(1).strip()
|
15 |
+
else:
|
16 |
+
json_str = markdown_text.strip()
|
17 |
+
|
18 |
+
try:
|
19 |
+
return json.loads(json_str)
|
20 |
+
except json.JSONDecodeError as e:
|
21 |
+
raise ValueError(f"Invalid JSON format: {e}")
|
22 |
+
|
23 |
+
def process_event(event):
|
24 |
+
openai = OpenAI(
|
25 |
+
api_key=os.environ.get('DEEP_API_KEY'),
|
26 |
+
base_url="https://api.deepinfra.com/v1/openai",
|
27 |
+
)
|
28 |
+
|
29 |
+
|
30 |
+
llm_prompt = f"""
|
31 |
+
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
|
32 |
+
|
33 |
+
{{
|
34 |
+
"market": "str",
|
35 |
+
"total_spend": "float",
|
36 |
+
"impressions": "float",
|
37 |
+
"clicks": "float",
|
38 |
+
"metrics_cpc": "float",
|
39 |
+
"metrics_cpm": "float",
|
40 |
+
"metrics_ctr": "float",
|
41 |
+
"metrics_cpa": "float",
|
42 |
+
"platform_spend_meta_total": "float",
|
43 |
+
"platform_spend_meta_instagram": "float",
|
44 |
+
"platform_spend_meta_facebook": "float",
|
45 |
+
"platform_spend_google_total": "float",
|
46 |
+
"platform_spend_google_youtube": "float",
|
47 |
+
"platform_spend_google_search_display": "float",
|
48 |
+
"platform_spend_programmatic": "float",
|
49 |
+
"revenue_average_ticket_price": "float",
|
50 |
+
"revenue_total_revenue": "float",
|
51 |
+
"revenue_roi": "float"
|
52 |
+
}}
|
53 |
+
|
54 |
+
Here is Text for it:
|
55 |
+
{event}
|
56 |
+
|
57 |
+
Return in only JSON adhering to Above Schema
|
58 |
+
"""
|
59 |
+
|
60 |
+
chat_completion = openai.chat.completions.create(
|
61 |
+
model="meta-llama/Llama-3.3-70B-Instruct-Turbo",
|
62 |
+
messages=[{"role": "user", "content": llm_prompt}],
|
63 |
+
)
|
64 |
+
return chat_completion.choices[0].message.content
|
65 |
+
|
66 |
+
def process_all_events(events):
|
67 |
+
json_all = []
|
68 |
+
progress_bar = st.progress(0)
|
69 |
+
|
70 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
|
71 |
+
futures = [executor.submit(process_event, event) for event in events]
|
72 |
+
|
73 |
+
for i, future in enumerate(concurrent.futures.as_completed(futures)):
|
74 |
+
progress = (i + 1) / len(events)
|
75 |
+
progress_bar.progress(progress)
|
76 |
+
st.write(f"Processed event {i + 1}/{len(events)}")
|
77 |
+
json_all.append(future.result())
|
78 |
+
|
79 |
+
return json_all
|
80 |
+
|
81 |
+
def main():
|
82 |
+
st.title("Rod Wave Concert Marketing Data Processor")
|
83 |
+
st.write("Upload a text file containing concert marketing data to convert it to CSV format")
|
84 |
+
|
85 |
+
uploaded_file = st.file_uploader("Choose a text file", type="txt")
|
86 |
+
|
87 |
+
if uploaded_file is not None:
|
88 |
+
text = uploaded_file.read().decode("utf-8")
|
89 |
+
events = re.split(r'\n(?=Rod Wave Concert)', text)
|
90 |
+
events = [event for event in events if event.strip()]
|
91 |
+
|
92 |
+
st.write(f"Found {len(events)} events to process")
|
93 |
+
|
94 |
+
if st.button("Process Data"):
|
95 |
+
with st.spinner("Processing events..."):
|
96 |
+
json_all = process_all_events(events)
|
97 |
+
|
98 |
+
json_sanity = []
|
99 |
+
for ele in json_all:
|
100 |
+
json_sanity.append(extract_and_parse_json_from_markdown(ele))
|
101 |
+
|
102 |
+
df = pd.DataFrame(json_sanity)
|
103 |
+
|
104 |
+
st.success("Processing complete!")
|
105 |
+
st.write("Preview of processed data:")
|
106 |
+
st.dataframe(df.head())
|
107 |
+
|
108 |
+
csv = df.to_csv(index=False)
|
109 |
+
st.download_button(
|
110 |
+
label="Download CSV",
|
111 |
+
data=csv,
|
112 |
+
file_name="processed_concert_data.csv",
|
113 |
+
mime="text/csv"
|
114 |
+
)
|
115 |
+
|
116 |
+
if __name__ == "__main__":
|
117 |
+
main()
|