File size: 6,225 Bytes
6ffb7a8
 
552eaff
 
 
aed26b5
 
 
552eaff
6ffb7a8
 
 
 
552eaff
 
 
 
 
 
 
 
6ffb7a8
 
 
552eaff
6ffb7a8
 
552eaff
 
6ffb7a8
552eaff
6ffb7a8
552eaff
 
 
aed26b5
552eaff
aed26b5
552eaff
 
 
 
 
 
6ffb7a8
552eaff
 
 
6ffb7a8
552eaff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aed26b5
6ffb7a8
aed26b5
552eaff
6ffb7a8
 
 
 
 
aed26b5
552eaff
6ffb7a8
 
aed26b5
552eaff
6ffb7a8
552eaff
 
 
 
 
 
 
 
 
6ffb7a8
 
 
552eaff
6ffb7a8
 
 
 
 
552eaff
6ffb7a8
552eaff
6ffb7a8
 
 
552eaff
6ffb7a8
 
552eaff
6ffb7a8
 
 
552eaff
6ffb7a8
 
 
 
552eaff
6ffb7a8
 
 
 
552eaff
 
 
 
 
 
 
 
 
 
 
aed26b5
552eaff
6ffb7a8
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
# app.py

#!pip install gradio transformers  # <--- เอาออกได้หากใช้ requirements.txt

import re
import gradio as gr
from transformers import pipeline

# โหลดโมเดลจาก Hugging Face (เลือกได้ตามต้องการ)
prompt_analyzer = pipeline("text2text-generation", model="google/flan-t5-base")

def analyze_prompt(user_prompt: str):
    """
    ฟังก์ชันนี้จะ:
      1. ตรวจสอบความว่างเปล่าของ Prompt
      2. ส่ง Prompt เข้าโมเดล text2text-generation เพื่อขอ:
         - Rating (1–10)
         - Feedback (เหตุผลหรือคำแนะนำ)
         - Improvements (ตัวอย่าง Prompt ที่ปรับปรุงแล้ว)
      3. แยกวิเคราะห์ (Parse) ข้อความที่โมเดลตอบมา ด้วย Regex
      4. คืนค่าผลลัพธ์เป็น (rating, feedback, suggestions)
    """
    
    if not user_prompt.strip():
        return "N/A", "กรุณากรอก Prompt ที่ถูกต้อง", "ไม่มีคำแนะนำ"
    
    instruction = f"""
    คุณคือนักออกแบบ Prompt มืออาชีพ
    โปรดวิเคราะห์ Prompt ด้านล่างสำหรับการสร้างภาพด้วย AI

    Prompt: {user_prompt}

    1. ให้คะแนน (Rating) Prompt นี้แบบเต็ม 10 คะแนน
    2. อธิบายสั้น ๆ ว่าทำไมถึงได้คะแนนนี้ (Feedback)
    3. เขียน Prompt ที่ปรับปรุงให้ดีขึ้น (Improvements) อย่างน้อย 3 รูปแบบ

    กรุณาเรียบเรียงผลลัพธ์ตามโครงสร้าง:

    Rating: X
    Feedback: (อธิบาย)
    Improvements:
    1. ...
    2. ...
    3. ...
    """

    # เรียกใช้งานโมเดล
    model_response = prompt_analyzer(instruction, max_length=300)[0]["generated_text"]
    
    # ใช้ Regex เพื่อค้นหา Rating, Feedback และ Improvements
    # โดยสมมติว่าตัวโมเดลจะมีคำว่า "Rating:", "Feedback:", และ "Improvements:" อยู่จริง
    rating_pattern = r"Rating:\s*(\d+)"
    feedback_pattern = r"Feedback:\s*(.*?)(?=Improvements:|$)"
    improvements_pattern = r"Improvements:\s*(.*)"

    rating_match = re.search(rating_pattern, model_response)
    feedback_match = re.search(feedback_pattern, model_response, re.DOTALL)
    improvements_match = re.search(improvements_pattern, model_response, re.DOTALL)

    if rating_match:
        rating = rating_match.group(1).strip()
    else:
        rating = "N/A"
        
    if feedback_match:
        feedback = feedback_match.group(1).strip()
    else:
        feedback = "ไม่พบคำแนะนำ"

    if improvements_match:
        suggestions = improvements_match.group(1).strip()
    else:
        suggestions = "ไม่พบคำแนะนำ"

    return rating, feedback, suggestions

# ตัวอย่าง Prompt ที่มีไว้ให้ผู้ใช้เลือก (Dropdown)
example_prompts = [
    "A majestic dragon soaring above a medieval castle, fantasy art style, highly detailed",
    "A peaceful countryside landscape with rolling hills and a small cottage at sunset",
    "A cyberpunk city scene with neon lights, flying cars, and towering skyscrapers",
]

# ฟังก์ชันสำหรับโหลด Prompt ตัวอย่าง
def set_example_prompt(example):
    return example

# สร้าง Gradio Interface
with gr.Blocks() as demo:
    gr.Markdown("""
    # แอปพลิเคชันเรียนรู้ Prompt Engineering แบบโต้ตอบ
    **(โดย สถาบัน Prompt Engineers Academy)**

    1. พิมพ์ Prompt ของคุณในช่องด้านล่าง  
    2. คลิก "Evaluate Prompt" เพื่อรับคะแนน (Rating), ข้อเสนอแนะ (Feedback) และตัวอย่าง Prompt ที่ปรับปรุงแล้ว (Improvements)  
    3. เลือก Prompt จากตัวอย่าง (Dropdown) เพื่อศึกษาเพิ่มเติมได้
    """)

    with gr.Row():
        with gr.Column():
            example_dropdown = gr.Dropdown(
                label="ตัวอย่าง Prompt ที่มีให้",
                choices=example_prompts,
                value=None,
                interactive=True
            )
            user_prompt_input = gr.Textbox(
                label="ใส่ Prompt ของคุณ:",
                lines=4,
                placeholder="เช่น 'A futuristic cityscape with neon lights at night, highly detailed...'"
            )
            load_example_btn = gr.Button("Load Example Prompt")
            analyze_btn = gr.Button("Evaluate Prompt")
        
        with gr.Column():
            score_output = gr.Textbox(
                label="คะแนน (Rating)",
                interactive=False
            )
            feedback_output = gr.Textbox(
                label="คำแนะนำ (Feedback)",
                lines=3,
                interactive=False
            )
            suggestions_output = gr.Textbox(
                label="Prompt ที่ปรับปรุงแล้ว (Improvements)",
                lines=6,
                interactive=False
            )
    
    load_example_btn.click(
        fn=set_example_prompt,
        inputs=[example_dropdown],
        outputs=[user_prompt_input]
    )

    analyze_btn.click(
        fn=analyze_prompt,
        inputs=[user_prompt_input],
        outputs=[score_output, feedback_output, suggestions_output]
    )

# เปิดใช้งานแอป Gradio
demo.launch()