Spaces:
Runtime error
Runtime error
| 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() |