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()