Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import random | |
| import time | |
| import json | |
| from datetime import datetime, timezone, timedelta | |
| def add_text(history, text): | |
| history = history + [(text, None)] | |
| return history, "" | |
| def bot(history): | |
| response = infer(history[-1][0], history) | |
| history[-1][1] = "" | |
| for character in response: | |
| history[-1][1] += character | |
| time.sleep(0.05) | |
| yield history | |
| # check profanity | |
| profanity = [ | |
| "ห่วย", "ซวย", "เฮงซวย", "โง่", "ไร้สาระ", | |
| "บัดซบ", "งี่เหง้า", "บ้า", "บ้าบอ", "น่าเบื่อ", | |
| "หลอกลวง", "เสียเวลา", "เท็จ", "งมงาย", "เลว", | |
| "เลวทราม", "แย่", "หลอกลวง", "โคตร", "เซ็ง", | |
| "เหี้ย" | |
| ] | |
| def profanity_check(text): | |
| global original_wish | |
| original_wish = text | |
| for word in profanity: | |
| if word in text: | |
| text = "[ข้อความมีคำหยาบ]" | |
| break | |
| return text | |
| response_all = [ | |
| "ขยัน พยายาม มุ่งมั่น อีกไม่นาน ท่านจะ (สมนึก สมหวัง สมคิด สมใจ สมเจตน์)", | |
| "เมื่อถึงเวลาเหมาะสม ก็จะสัมฤทธิ์ผล ตามที่ท่านปราถนา", | |
| "พรที่ท่านขอ จะค่อยๆเห็นผล เมื่อท่านทำแต่กรรมดี", | |
| "ความปราถนาของท่าน ยังไม่ถึงเวลา ใจเย็นๆ รอก่อน", | |
| "ความเหนื่อยยากของท่าน จะไม่สูญเปล่าอย่างแน่นอน", | |
| "จงทำแต่กรรมดี ผลบุญจะช่วยส่งเสริมให้ท่านเจริญรุ่งเรือง", | |
| "ความดีที่ท่านสะสมมา เมื่อถึงเวลา ท่านจะได้รับรางวัลอย่างงาม", | |
| "สิ่งที่ท่านปราถนา ท่านจะสมหวังเมื่อถึงเวลา", | |
| "พรที่ท่านขอ ลองคิดใหม่อีกครั้ง", | |
| "อะไรที่รีบร้อน มักพลาด ใจเย็นๆทุกอย่างจะลงตัวด้วยดี", | |
| "ไม่เคยมีใคร ทำความดีอย่างบริสุทธิ์ใจแล้วไม่เห็นผล", | |
| "คิดดี พูดดี ทำดี ผลย่อมดีเสมอ", | |
| "จงสงบนิ่ง ค่อยๆย้อนคิด ลำดับการกระทำที่ผ่านมา ท่านอาจต้องแก้ไข เปลี่ยนแปลง เพื่อให้ทุกอย่างลงตัวด้วยดี", | |
| "ขอให้ท่านใจเย็นๆ คิดดี ทำดี แล้วทุกอย่างจะสมปราถนาเอง", | |
| "ทุกอย่างบนโลก อยู่ในเหตุผล ท่านทำแต่ความดี ผลย่อมดีเสมอ", | |
| "กฎแห่งกรรม กำหนดทุกอย่าง ขอให้ทำแต่กรรมดี", | |
| "พรที่ท่านขอ จะค่อยๆเห็นผล ถ้าท่านทำแต่ความดี", | |
| "ถ้าท่านทำความดีมากกว่านี้ ท่านอาจได้ลัดคิวถูกรางวัลที่ 1", | |
| "พรที่ท่านขอยังไม่ถึงเวลา แต่ท่านจะได้รับสิ่งดีๆอย่างอื่นแทน", | |
| "ในไม่ช้า คุณจะโชคดี สมปราถนา จงทำแต่กรรมดี", | |
| "ความโชคดี อยู่รอบตัวท่าน ใกล้ถึงคิวแล้ว รอหน่อย", | |
| "อย่าเครียด อย่ากังวล สิ่งดีๆกำลังจะเข้ามาในชีวิตแล้ว", | |
| "โลกนี้ไม่มีอะไรแน่นอน แต่ท่านต้องประสบความสำเร็จแน่นอน", | |
| "ช่วงนี้อยู่เฉยๆ นิ่งๆ ใช้สติแล้วทุกอย่างจะสมหวัง", | |
| ] | |
| # Set all responses | |
| response_list = response_all.copy() | |
| def save_history(history, response): | |
| # Get the current date and time in the Thai time zone | |
| thai_timezone = timezone(timedelta(hours=7)) # Thailand is UTC+7 | |
| current_time = datetime.now(thai_timezone) | |
| # Format the current time as a string | |
| timestamp_str = current_time.strftime('%Y-%m-%d_%H-%M-%S') | |
| # Create the file name with the timestamp | |
| file_name = f'{timestamp_str}.json' | |
| # Create log | |
| history_log = [history, response] | |
| # Save the history list to a JSON file | |
| with open(file_name, 'w', encoding='utf-8') as json_file: | |
| json.dump(history_log, json_file, ensure_ascii=False, indent=4) | |
| def infer(wish, history): | |
| global response_list | |
| if "[ข้อความมีคำหยาบ]" in wish: | |
| response = "ขอให้ท่านส่งมาขอพรใหม่ เนื่องจากบางคำพูดไม่สุภาพ" | |
| last_history = history[-1][0] + original_wish | |
| save_history(last_history, response) | |
| else: | |
| response = random.choice(response_list) | |
| response_list.remove(response) | |
| if not response_list: | |
| response_list = response_all.copy() | |
| last_history = history[-1][0] | |
| save_history(last_history, response) | |
| return response | |
| # yellow #E6DE77, pink #F8C8DC, skyblue #A0D8EF | |
| css=""" | |
| .gradio-container { | |
| background-image: url("https://huggingface.co/spaces/jingwora/Guan-Yin-Bless-You/resolve/main/bg-01.jpg"); | |
| height: 500px; | |
| background-position: center; | |
| background-repeat: no-repeat; | |
| background-size: cover; | |
| background-color: #A0D8EF; | |
| } | |
| #col-container { | |
| max-width: 700px; | |
| margin-left: auto; | |
| margin-right: auto; | |
| } | |
| #button { | |
| border-color: #EBB40F; | |
| border-width: thick; | |
| } | |
| """ | |
| title = """ | |
| <center><div style="text-align: center;max-width: 700px;"> | |
| <h1>เจ้าแม่กวนอิมให้พร</h1> | |
| <h3>"โปรดบอกความปรารถนาของท่าน"</h3> | |
| </div> | |
| </center> | |
| """ | |
| instruction = """ | |
| <center><p>วิธีขอพร: 1.พิมพ์ในช่องความปรารถนา 2.กดปุ่มขอพร</p></center> | |
| """ | |
| note = """ | |
| <div style="text-align: left;max-width: 700px;"> | |
| <h3>🚩จุดประสงค์ที่ทำAIเจ้าแม่กวนอิมให้พร</h3> | |
| <p> | |
| - ให้สร้างแต่กรรมดี<br> | |
| - ให้กำลังใจ<br> | |
| - ให้เป็นแรงบันดาลใจ<br> | |
| - ให้เป็นที่พึ่งทางใจ<br> | |
| </p> | |
| <br> | |
| <h3>💡ให้ข้อคิด</h3> | |
| <p> | |
| - ทุกชีวิต ทุกข์ สุข ร้องไห้ หัวเราะ คือสัจจธรรม<br> | |
| - การทำความดี ไม่มีคำว่าสาย เริ่มต้นเดี๋ยวนี้<br> | |
| - ทุกชีวิตบนโลก ต้องดิ้นรน ต่อสู่ เพื่อความอยู่รอด มันคือกฎแห่ง ธรรมชาติ<br> | |
| - ถ้ารู้ว่า คิดผิด เดินทางผิด รีบเปลี่ยนแปลง ก่อนจะสายไป<br> | |
| - ฉลาด สุขุม รอบคอบ อ่อนน้อม รู้คุณคน ไม่ประมาท ไม่อวดดี ชีวิตจะรุ่งเรือง<br> | |
| </p> | |
| <br> | |
| <h3>📝ข้อกำหนด</h3> | |
| <p> | |
| - ขอพรใช้คำพูดไม่เกิน 20 คำ<br> | |
| - ทางเราจะไม่รับผิดชอบ กรณีที่ท่านขอพรแล้วไม่ได้ดั่งใจ เพราะทุกอย่างอยู่ภายใต้กฎแห่งกรรม<br> | |
| - อย่ามีใจคิดร้ายผู้อื่น อิจฉาผู้อื่น เพราะอาจทำให้ท่านขอพรแล้วไม่ได้ผล<br> | |
| - อย่าขออะไรที่ผิดคุณธรรม ผิดกฎหมาย ผิดธรรมชาติ<br> | |
| - อย่าใช้คำพูดที่ไม่สุภาพในการขอพร<br> | |
| </p> | |
| </div> | |
| """ | |
| version = """ | |
| <div style="text-align: center;max-width: 700px;"> | |
| <p style="text-align: left;"> | |
| version: 0.10 | |
| </p> | |
| </div> | |
| """ | |
| guanyin = '<center><img src="https://huggingface.co/spaces/jingwora/Guan-Yin-Bless-You/resolve/main/guan-yin-01.gif" alt="guan-yin-01.gif" style="width:400px;"></center>' | |
| with gr.Blocks(css=css) as demo: | |
| with gr.Column(elem_id="col-container"): | |
| gr.HTML(title) | |
| gr.HTML(guanyin) | |
| gr.HTML(instruction) | |
| with gr.Row(equal_height=True): | |
| wish = gr.Textbox(show_label=False, placeholder="ความปรารถนา", scale=10) | |
| submit_btn = gr.Button(value="✨ขอพร✨", elem_id="button", scale=1) | |
| chatbot = gr.Chatbot([], elem_id="chatbot") | |
| gr.HTML(note) | |
| gr.HTML(version) | |
| submit_btn.click(profanity_check, [wish], [wish]).then(add_text, [chatbot, wish], [chatbot, wish]).then( | |
| bot, chatbot, chatbot) | |
| demo.queue(concurrency_count=5, max_size=20).launch(debug=True) |