YAML Metadata Warning:empty or missing yaml metadata in repo card
Check out the documentation for more information.
license: llama2 ---import os import re import gradio as gr from huggingface_hub import InferenceClient
You may change the model if needed
MODEL_ID = os.getenv( "MODEL_ID", "mistralai/Mistral-7B-Instruct-v0.3:fastest" )
HF_TOKEN = os.getenv("HF_TOKEN")
client = InferenceClient(api_key=HF_TOKEN)
STOP_WORDS = { "هذا", "هذه", "ذلك", "التي", "الذي", "على", "من", "إلى", "في", "عن", "مع", "هل", "كيف", "ما", "اكتب", "please", "write", "about", "the", "and", "for", "with" }
def tokenize(text): words = re.findall(r"[\u0600-\u06FFa-zA-Z]{3,}", text.lower()) return [word for word in words if word not in STOP_WORDS]
def evaluate_response(prompt, answer): prompt_words = set(tokenize(prompt)) answer_words = set(tokenize(answer)) common_words = prompt_words.intersection(answer_words)
score = 0
reasons = []
if len(answer.strip()) >= 30:
score += 30
reasons.append("الرد يحتوي على محتوى كافٍ.")
else:
reasons.append("الرد قصير جدًا.")
if len(answer_words) >= 8:
score += 30
reasons.append("الرد يحتوي على شرح مناسب.")
else:
reasons.append("الرد يحتاج إلى مزيد من التفاصيل.")
if len(common_words) >= 1:
score += 40
reasons.append("الرد يتضمن كلمات مرتبطة بالـprompt.")
else:
reasons.append("لا توجد كلمات مشتركة كافية بين الطلب والرد.")
if score >= 70:
result = "✔ النموذج فهم الـprompt بشكل جيد."
elif score >= 40:
result = "⚠ النموذج فهم جزءًا من الـprompt، لكن الرد يحتاج تحسينًا."
else:
result = "❌ النموذج لم يفهم الـprompt بشكل كافٍ."
return score, result, reasons
def generate_and_evaluate(prompt): if not prompt or not prompt.strip(): return "", "⚠ يرجى كتابة Prompt أولًا."
try:
completion = client.chat_completion(
model=MODEL_ID,
messages=[
{
"role": "system",
"content": "Answer clearly, accurately, and directly. Use the same language as the user."
},
{
"role": "user",
"content": prompt
}
],
max_tokens=350,
temperature=0.4
)
answer = completion.choices[0].message.content
score, result, reasons = evaluate_response(prompt, answer)
evaluation = f"""
التقييم التلقائي
النتيجة: {result}
الدرجة: {score}/100
سبب التقييم:
- {reasons[0]}
- {reasons[1]}
- {reasons[2]}
ملاحظة: هذا تقييم مبسط يعتمد على طول الرد وارتباطه بكلمات الـprompt، وليس حكمًا نهائيًا على جودة النموذج. """ return answer, evaluation
except Exception as error:
return "", f"❌ حدث خطأ: {str(error)}"
with gr.Blocks(theme=gr.themes.Soft(), title="Prompt Understanding Test") as demo: gr.Markdown(""" # 🧠 Prompt Understanding Test أدخل سؤالًا أو تعليمات، ثم شاهد رد النموذج والتقييم التلقائي لفهمه للـprompt. """)
prompt = gr.Textbox(
label="اكتب الـPrompt",
placeholder="مثال: اشرح أهمية شرب الماء بطريقة بسيطة.",
lines=5
)
run_button = gr.Button("اختبر فهم النموذج", variant="primary")
answer = gr.Textbox(
label="رد النموذج",
lines=10
gradio>=5.0 huggingface_hub>=0.30.0