zenityx/Interactive_Prompt_Learning_Tool
0
1# app.py2 3#!pip install gradio transformers # <--- เอาออกได้หากใช้ requirements.txt4 5import re6import gradio as gr7from transformers import pipeline8 9# โหลดโมเดลจาก Hugging Face (เลือกได้ตามต้องการ)10prompt_analyzer = pipeline("text2text-generation", model="google/flan-t5-base")11 12def analyze_prompt(user_prompt: str):13 """14 ฟังก์ชันนี้จะ:15 1. ตรวจสอบความว่างเปล่าของ Prompt16 2. ส่ง Prompt เข้าโมเดล text2text-generation เพื่อขอ:17 - Rating (1–10)18 - Feedback (เหตุผลหรือคำแนะนำ)19 - Improvements (ตัวอย่าง Prompt ที่ปรับปรุงแล้ว)20 3. แยกวิเคราะห์ (Parse) ข้อความที่โมเดลตอบมา ด้วย Regex21 4. คืนค่าผลลัพธ์เป็น (rating, feedback, suggestions)22 """23 24 if not user_prompt.strip():25 return "N/A", "กรุณากรอก Prompt ที่ถูกต้อง", "ไม่มีคำแนะนำ"26 27 instruction = f"""28 คุณคือนักออกแบบ Prompt มืออาชีพ29 โปรดวิเคราะห์ Prompt ด้านล่างสำหรับการสร้างภาพด้วย AI30 31 Prompt: {user_prompt}32 33 1. ให้คะแนน (Rating) Prompt นี้แบบเต็ม 10 คะแนน34 2. อธิบายสั้น ๆ ว่าทำไมถึงได้คะแนนนี้ (Feedback)35 3. เขียน Prompt ที่ปรับปรุงให้ดีขึ้น (Improvements) อย่างน้อย 3 รูปแบบ36 37 กรุณาเรียบเรียงผลลัพธ์ตามโครงสร้าง:38 39 Rating: X40 Feedback: (อธิบาย)41 Improvements:42 1. ...43 2. ...44 3. ...45 """46 47 # เรียกใช้งานโมเดล48 model_response = prompt_analyzer(instruction, max_length=300)[0]["generated_text"]49 50 # ใช้ Regex เพื่อค้นหา Rating, Feedback และ Improvements51 # โดยสมมติว่าตัวโมเดลจะมีคำว่า "Rating:", "Feedback:", และ "Improvements:" อยู่จริง52 rating_pattern = r"Rating:\s*(\d+)"53 feedback_pattern = r"Feedback:\s*(.*?)(?=Improvements:|$)"54 improvements_pattern = r"Improvements:\s*(.*)"55 56 rating_match = re.search(rating_pattern, model_response)57 feedback_match = re.search(feedback_pattern, model_response, re.DOTALL)58 improvements_match = re.search(improvements_pattern, model_response, re.DOTALL)59 60 if rating_match:61 rating = rating_match.group(1).strip()62 else:63 rating = "N/A"64 65 if feedback_match:66 feedback = feedback_match.group(1).strip()67 else:68 feedback = "ไม่พบคำแนะนำ"69 70 if improvements_match:71 suggestions = improvements_match.group(1).strip()72 else:73 suggestions = "ไม่พบคำแนะนำ"74 75 return rating, feedback, suggestions76 77# ตัวอย่าง Prompt ที่มีไว้ให้ผู้ใช้เลือก (Dropdown)78example_prompts = [79 "A majestic dragon soaring above a medieval castle, fantasy art style, highly detailed",80 "A peaceful countryside landscape with rolling hills and a small cottage at sunset",81 "A cyberpunk city scene with neon lights, flying cars, and towering skyscrapers",82]83 84# ฟังก์ชันสำหรับโหลด Prompt ตัวอย่าง85def set_example_prompt(example):86 return example87 88# สร้าง Gradio Interface89with gr.Blocks() as demo:90 gr.Markdown("""91 # แอปพลิเคชันเรียนรู้ Prompt Engineering แบบโต้ตอบ92 **(โดย สถาบัน Prompt Engineers Academy)**93 94 1. พิมพ์ Prompt ของคุณในช่องด้านล่าง 95 2. คลิก "Evaluate Prompt" เพื่อรับคะแนน (Rating), ข้อเสนอแนะ (Feedback) และตัวอย่าง Prompt ที่ปรับปรุงแล้ว (Improvements) 96 3. เลือก Prompt จากตัวอย่าง (Dropdown) เพื่อศึกษาเพิ่มเติมได้97 """)98 99 with gr.Row():100 with gr.Column():101 example_dropdown = gr.Dropdown(102 label="ตัวอย่าง Prompt ที่มีให้",103 choices=example_prompts,104 value=None,105 interactive=True106 )107 user_prompt_input = gr.Textbox(108 label="ใส่ Prompt ของคุณ:",109 lines=4,110 placeholder="เช่น 'A futuristic cityscape with neon lights at night, highly detailed...'"111 )112 load_example_btn = gr.Button("Load Example Prompt")113 analyze_btn = gr.Button("Evaluate Prompt")114 115 with gr.Column():116 score_output = gr.Textbox(117 label="คะแนน (Rating)",118 interactive=False119 )120 feedback_output = gr.Textbox(121 label="คำแนะนำ (Feedback)",122 lines=3,123 interactive=False124 )125 suggestions_output = gr.Textbox(126 label="Prompt ที่ปรับปรุงแล้ว (Improvements)",127 lines=6,128 interactive=False129 )130 131 load_example_btn.click(132 fn=set_example_prompt,133 inputs=[example_dropdown],134 outputs=[user_prompt_input]135 )136 137 analyze_btn.click(138 fn=analyze_prompt,139 inputs=[user_prompt_input],140 outputs=[score_output, feedback_output, suggestions_output]141 )142 143# เปิดใช้งานแอป Gradio144demo.launch()145 