prompt / app.py
yumna7's picture
Update app.py
084de80 verified
import gradio as gr
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
import torch
import re
# ๐Ÿ”น ู…ูˆุฏูŠู„ ู…ุฌุงู†ูŠ ูˆู…ูุชูˆุญ
MODEL_NAME = "google/flan-t5-base"
print("ุชุญู…ูŠู„ ุงู„ู…ูˆุฏูŠู„ุŒ ุงู†ุชุธุฑ ู‚ู„ูŠู„ุงู‹...")
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME, device_map="auto")
# ๐Ÿ”น ุฏุงู„ุฉ ู„ุชุญูˆูŠู„ ุชู‚ูŠูŠู… ูˆุตููŠ ู„ู†ุณุจุฉ ู…ุฆูˆูŠุฉ
def map_to_percentage(text):
text = text.lower()
if any(w in text for w in ["high", "ุฌูŠุฏ", "ู…ู…ุชุงุฒ"]):
return "90%"
elif any(w in text for w in ["medium", "ู…ุชูˆุณุท"]):
return "60%"
elif any(w in text for w in ["low", "ุถุนูŠู"]):
return "30%"
else:
return "N/A"
# ๐Ÿ”น ุฏุงู„ุฉ ุชู‚ูŠูŠู… Prompts
def evaluate_prompt(prompts_text):
lines = [p.strip() for p in prompts_text.splitlines() if p.strip()]
results = []
for p in lines:
eval_text = f"""
Evaluate the following Prompt in terms of clarity, consistency, efficiency, extensibility, and user experience.
Choose **one word only**: Low, Medium, High.
Prompt: {p}
Answer with a single word only.
"""
inputs = tokenizer(eval_text, return_tensors="pt").to(model.device)
output = model.generate(**inputs, max_new_tokens=70)
decoded = tokenizer.decode(output[0], skip_special_tokens=True)
# ุชุญูˆูŠู„ ุงู„ุชู‚ูŠูŠู… ุงู„ูˆุตููŠ ู„ู†ุณุจุฉ ู…ุฆูˆูŠุฉ
score = map_to_percentage(decoded)
results.append(f"{p} โ†’ {score}")
return "\n".join(results)
# ๐Ÿ”น ูˆุงุฌู‡ุฉ Gradio
iface = gr.Interface(
fn=evaluate_prompt,
inputs=gr.Textbox(lines=15, placeholder="ุงูƒุชุจ ูƒู„ Prompt ููŠ ุณุทุฑ ู…ู†ูุตู„"),
outputs="textbox",
title="Prompt Evaluator ุจุฏูˆู† API",
description="ุฃุฏุฎู„ ุนุฏุฉ Prompts ูˆุณูŠุนุทูŠ ูƒู„ ูˆุงุญุฏ ู†ุณุจุฉ ู…ุฆูˆูŠุฉ ุชู‚ุฑูŠุจูŠุฉ ู„ู„ุชู‚ูŠูŠู…."
)
iface.launch(share=True)