zenityx's picture
Update app.py
552eaff verified
# 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()