Spaces:
Runtime error
Runtime error
File size: 1,869 Bytes
f5a0e69 e184b95 f5a0e69 e184b95 |
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 |
import gradio as gr
from transformers import pipeline
from sentence_transformers import SentenceTransformer, util
import torch
# ุชุญู
ูู ูู
ูุฐุฌ ุงูุชุถู
ูู ูุชูููู
ุงูู
ุนูู
embedder = SentenceTransformer('all-MiniLM-L6-v2')
# ุชุญู
ูู ุงููู
ูุฐุฌ ุงููุบูู ุนุจุฑ pipeline
# ูู
ูู ุชุบููุฑู ุฅูู LLaMA-2 ุฃู ุฃู ูู
ูุฐุฌ ู
ุชูุงูู ู
ุน HF
generator = pipeline(
"text-generation",
model="mistralai/Mistral-7B-Instruct-v0.1",
tokenizer="mistralai/Mistral-7B-Instruct-v0.1",
device=0 if torch.cuda.is_available() else -1,
max_new_tokens=150,
do_sample=True,
temperature=0.7
)
# ุฏุงูุฉ ูุชูููู
ุงูููู
(ุชุดุงุจู ุงูุฌู
ูุฉ ูุงูุฑุฏ)
def evaluate_understanding(prompt, response):
prompt_emb = embedder.encode(prompt, convert_to_tensor=True)
response_emb = embedder.encode(response, convert_to_tensor=True)
similarity = util.cos_sim(prompt_emb, response_emb).item()
status = "โ
ู
ูููู
ุฌูุฏูุง" if similarity > 0.5 else "โ ูู
ููููู
ุฌูุฏูุง"
return status + f" (ุฏุฑุฌุฉ ุงูุชุดุงุจู: {similarity:.2f})"
# ุฏุงูุฉ ุงูุชุทุจูู ุงูุฃุณุงุณูุฉ
def generate_and_evaluate(prompt):
result = generator(prompt)[0]["generated_text"]
evaluation = evaluate_understanding(prompt, result)
return result, evaluation
# ูุงุฌูุฉ Gradio
iface = gr.Interface(
fn=generate_and_evaluate,
inputs=gr.Textbox(label="๐ ุฃุฏุฎู ุชุนููู
ุงุช ุฃู ุณุคุงู (Prompt)"),
outputs=[
gr.Textbox(label="๐ค ุฑุฏ ุงููู
ูุฐุฌ"),
gr.Textbox(label="๐ ุชูููู
ุงูููู
"),
],
title="๐ง LLM Prompt Understanding Evaluator",
description="ุฃุฏุฎู ุณุคุงูุงู ุฃู ุชุนููู
ุงุชุ ูุณูุชู
ุชูููุฏ ุงูุฑุฏ ูุชูููู
ู
ุฏู ููู
ุงููู
ูุฐุฌ ูู ุชููุงุฆููุง."
)
iface.launch()
|