Spaces:
Runtime error
Runtime error
File size: 10,089 Bytes
5a8627d de93fff 5a8627d de93fff de0666f de93fff 633c8fa b36b9b1 de93fff 633c8fa b36b9b1 de93fff 5a8627d de93fff 5a8627d de93fff 5a8627d de93fff 5a8627d de93fff 5a8627d de93fff 5a8627d de93fff 5a8627d de93fff 5a8627d de93fff 5a8627d d799e35 5a8627d d799e35 5a8627d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | import gradio as gr
import requests
import json
import line_fucntion as LINE
import pandas as pd
LINE_AUDIENCE_UPLOAD = "https://api.line.me/v2/bot/audienceGroup/upload"
LINE_NARROWCAST = "https://api.line.me/v2/bot/message/narrowcast"
LINE_REQUEST_DATA = "https://api.line.me/v2/bot/insight/message/event?requestId="
LINE_AUDIENCE_DATA = "https://api.line.me/v2/bot/audienceGroup/"
LINE_DELETE_AUDIENCE = "https://api.line.me/v2/bot/audienceGroup"
LINE_PUSH = "https://api.line.me/v2/bot/message/push"
def push_manneger(line_access_token, file_option, list_uid, line_uid_file, option, image_address, image_alt, image_width, image_height, text_message=None, endpoint_url=None, dynamic_parameter=None):
if file_option == "Text":
list_uid_to_push = list_uid.split("\n")
else:
if line_uid_file is None:
return "Error: No LINE UID file uploaded."
else:
file_path = line_uid_file.name
df = pd.read_csv(file_path)
list_uid_to_push = df['line_uid'].unique()
if list_uid_to_push:
headers = LINE.line_header(line_access_token)
amount_to_push = len(list_uid_to_push)
push_result = 0
if "?" in endpoint_url:
final_url = endpoint_url + "&"
else:
final_url = endpoint_url + "?"
if option == "Single Image map":
for uid in list_uid_to_push:
final_url_with_uid = final_url + f"{dynamic_parameter}=" + str(uid)
image_object = LINE.image_map_single(altText=image_alt,
imageLink=image_address,
ladingpage=final_url_with_uid,
width=image_width,
height=image_height
)
push_message = LINE.message_push(to=uid, messageObject=[image_object])
res = requests.post(LINE_PUSH, headers=headers, json=push_message)
if res.status_code == 200:
push_result += 1
message_result = f'''Total LINE UID: {amount_to_push}\nTotal push success: {push_result}'''
return message_result
elif option == "Image map with text":
for uid in list_uid_to_push:
final_url_with_uid = final_url + f"{dynamic_parameter}=" + str(uid)
image_object = LINE.image_map_single(altText=image_alt,
imageLink=image_address,
ladingpage=final_url_with_uid,
width=image_width,
height=image_height
)
text_object = LINE.single_message(text_message)
push_message = LINE.message_push(to=uid, messageObject=[image_object, text_object])
res = requests.post(LINE_PUSH, headers=headers, json=push_message)
if res.status_code == 200:
push_result += 1
message_result = f'''Total LINE UID: {amount_to_push}\nTotal push success: {push_result}'''
return message_result
elif option == "Text with Image map":
for uid in list_uid_to_push:
final_url_with_uid = final_url + f"{dynamic_parameter}=" + str(uid)
image_object = LINE.image_map_single(altText=image_alt,
imageLink=image_address,
ladingpage=final_url_with_uid,
width=image_width,
height=image_height
)
text_object = LINE.single_message(text_message)
push_message = LINE.message_push(to=uid, messageObject=[text_object, image_object])
res = requests.post(LINE_PUSH, headers=headers, json=push_message)
if res.status_code == 200:
push_result += 1
message_result = f'''Total LINE UID: {amount_to_push}\nTotal push success: {push_result}'''
return message_result
else:
return '''Out of condition, Please select message object in "Single Image", "Image with text", "Text with Image"'''
def reset_inputs():
return "", None, "Single Image", "", "", "" # Reset inputs to default values
# Gradio UI Layout
def main():
with gr.Blocks() as ui:
gr.Markdown("# Happy LINE Service: LINE Personlaized boardcast")
# User input fields
line_access_token = gr.Textbox(label="LINE Channel Access Token")
audience_main_id = gr.Textbox(label="Main LINE Audience ID")
with gr.Row():
age_min = gr.Dropdown([15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70],label="อายุขั้นต่ำ: ปี")
age_max= gr.Dropdown([15 ,20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70],label="อายุสูงไม่เกิน: ปี")
# age = gr.Slider(minimum=15, maximum=70, step=15,label='Age', info="Choose age range", interactive=True)
with gr.Row():
friend_duration_min = gr.Dropdown([7, 30, 90, 180, 365],label="ระยะเวลาที่เพิ่มเพื่อนขั้นต่ำ: วัน")
friend_duration_max = gr.Dropdown([7, 30, 90, 180, 365],label="ระยะเวลาที่เพิ่มเพื่อนไม่เกิน: วัน")
# friend_duration = gr.Slider(minimum=30, maximum=180, step=30, label="Friend Duration", info="Choose friend duration", randomize=True)
with gr.Row():
gender = gr.CheckboxGroup(["Male", "Female"], label="เพศ")
system = gr.CheckboxGroup(["ios", "Android"], label='ระบบปฏิบัติการ')
region = gr.CheckboxGroup(['Bangkok', 'Pattaya', 'Northern', 'Central', 'Southern', 'Eastern', 'NorthEastern', 'Western'], label="ภูมิภาค")
with gr.Row():
option = gr.Radio(label="Select Option", choices=["ข้อความแบบเดี่ยว","รูปภาพแบบเดี่ยว", "รูปภาพ ตามด้วยข้อความ", "ข้อความ ตามด้วยรูปภาพ"])
# Message Conditional Inputs
image_address = gr.Textbox(label="Your Image address", visible=False)
image_alt = gr.Textbox(label="Your Image ALT", visible=False)
image_width= gr.Textbox(label="Your Image Width", visible=False)
image_height= gr.Textbox(label="Your Image Height", visible=False)
text_message = gr.Textbox(label="Your Text message", visible=False)
endpoint_url = gr.Textbox(label="Your Endpint URL", visible=False)
def toggle_inputs_file(option):
if option == "Text":
return (
gr.update(visible=True), gr.update(visible=False)
)
else:
return (
gr.update(visible=False), gr.update(visible=True)
)
def toggle_inputs(option):
if option == "ข้อความแบบเดี่ยว":
return (
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
)
elif option == "รูปภาพแบบเดี่ยว":
return (
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True),
gr.update(visible=True), gr.update(visible=False), gr.update(visible=True)
)
elif option == "รูปภาพ ตามด้วยข้อความ":
return (
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True),
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
)
elif option == "ข้อความ ตามด้วยรูปภาพ":
return (
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True),
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
)
option.change(fn=toggle_inputs, inputs=option, outputs=[image_address, image_alt, image_width, image_height, text_message, endpoint_url])
# # Submit button and output
submit_btn = gr.Button("Submit")
reset_btn = gr.Button("Reset")
output = gr.Textbox(label="Result", interactive=False)
# submit_btn.click(fn=push_manneger,
# inputs=[line_access_token, file_option, list_uid, line_uid_file, option, image_address, image_alt, image_width, image_height, text_message, endpoint_url, dynamic_parameter],
# outputs=output)
# # Connect the reset function to Gradio UI
# reset_btn.click(fn=reset_inputs,
# inputs=[],
# outputs=[line_access_token, file_option, list_uid,line_uid_file, option, image_address, image_alt, image_width, image_height, text_message, endpoint_url, dynamic_parameter, output])
ui.launch()
if __name__ == "__main__":
main() |