Spaces:
Runtime error
Runtime error
Commit ·
e6b34ce
1
Parent(s): ff4ce05
mistaralai 사용
Browse files- model_comparion.py +88 -0
- model_comparion2.py +79 -0
model_comparion.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
# ✅ 고성능 모델 설정
|
| 6 |
+
MODEL_NAME = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
| 7 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 8 |
+
|
| 9 |
+
# ✅ 토크나이저 및 모델 로드
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_fast=True)
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
+
MODEL_NAME,
|
| 13 |
+
torch_dtype=torch.float16,
|
| 14 |
+
device_map="auto",
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# ✅ 프롬프트 생성 함수
|
| 18 |
+
def build_comparison_prompt(story_a, story_b):
|
| 19 |
+
return f"""<s>[INST] You are a children's story evaluation expert.
|
| 20 |
+
|
| 21 |
+
Compare the following two stories on:
|
| 22 |
+
1. Coherence (structure and flow)
|
| 23 |
+
2. Creativity (imagination and originality)
|
| 24 |
+
3. Engagement (fun and emotional draw)
|
| 25 |
+
|
| 26 |
+
Evaluate both stories and score each criterion from 1 to 5.
|
| 27 |
+
Then, provide a brief comment and declare the overall better story.
|
| 28 |
+
|
| 29 |
+
Story A:
|
| 30 |
+
{story_a}
|
| 31 |
+
|
| 32 |
+
Story B:
|
| 33 |
+
{story_b}
|
| 34 |
+
|
| 35 |
+
Respond in this format:
|
| 36 |
+
|
| 37 |
+
Story A:
|
| 38 |
+
- Coherence: ?/5
|
| 39 |
+
- Creativity: ?/5
|
| 40 |
+
- Engagement: ?/5
|
| 41 |
+
- Comment: ...
|
| 42 |
+
|
| 43 |
+
Story B:
|
| 44 |
+
- Coherence: ?/5
|
| 45 |
+
- Creativity: ?/5
|
| 46 |
+
- Engagement: ?/5
|
| 47 |
+
- Comment: ...
|
| 48 |
+
|
| 49 |
+
🟢 Overall Winner: Story A or Story B
|
| 50 |
+
Comment: ... [/INST]
|
| 51 |
+
"""
|
| 52 |
+
|
| 53 |
+
# ✅ 평가 함수
|
| 54 |
+
def evaluate_stories_with_mixtral(story_a, story_b):
|
| 55 |
+
prompt = build_comparison_prompt(story_a, story_b)
|
| 56 |
+
inputs = tokenizer(prompt, return_tensors="pt", return_token_type_ids=False).to(device)
|
| 57 |
+
|
| 58 |
+
outputs = model.generate(
|
| 59 |
+
**inputs,
|
| 60 |
+
max_new_tokens=1024,
|
| 61 |
+
temperature=0.7,
|
| 62 |
+
top_p=0.9,
|
| 63 |
+
do_sample=True,
|
| 64 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 68 |
+
result = result.replace(prompt.strip(), "").strip() # 프롬프트 제거
|
| 69 |
+
|
| 70 |
+
if len(result) < 30 or "Story A" not in result:
|
| 71 |
+
return "❌ 평가 실패: 출력이 부족하거나 형식이 맞지 않습니다. 다시 시도해주세요."
|
| 72 |
+
return result
|
| 73 |
+
|
| 74 |
+
# ✅ Gradio UI
|
| 75 |
+
with gr.Blocks() as demo:
|
| 76 |
+
gr.Markdown("## 🧙♂️ 동화 비교 평가기 (Mixtral 8x7B)\n긴 동화도 안정적으로 비교합니다!")
|
| 77 |
+
|
| 78 |
+
with gr.Row():
|
| 79 |
+
story_a = gr.Textbox(label="📘 Story A", lines=20, placeholder="전체 동화 A 입력")
|
| 80 |
+
story_b = gr.Textbox(label="📗 Story B", lines=20, placeholder="전체 동화 B 입력")
|
| 81 |
+
|
| 82 |
+
result = gr.Textbox(label="📊 평가 결과", lines=35)
|
| 83 |
+
compare = gr.Button("🧠 평가하기")
|
| 84 |
+
|
| 85 |
+
compare.click(fn=evaluate_stories_with_mixtral, inputs=[story_a, story_b], outputs=result)
|
| 86 |
+
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
demo.launch(share=True)
|
model_comparion2.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
+
|
| 5 |
+
# ✅ 고성능 모델 지정
|
| 6 |
+
MODEL_NAME = "google/flan-t5-large"
|
| 7 |
+
|
| 8 |
+
# ✅ 디바이스 설정
|
| 9 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 10 |
+
|
| 11 |
+
# ✅ 모델 및 토크나이저 로드
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 13 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME).to(device)
|
| 14 |
+
|
| 15 |
+
# ✅ 프롬프트 생성
|
| 16 |
+
def build_prompt(story_a, story_b):
|
| 17 |
+
return f"""
|
| 18 |
+
You are a story evaluation assistant. Please compare the two children's stories below using the following criteria:
|
| 19 |
+
1. Coherence (structure and flow)
|
| 20 |
+
2. Creativity (imagination and originality)
|
| 21 |
+
3. Engagement (fun and emotional draw)
|
| 22 |
+
|
| 23 |
+
Give each story a score from 1 to 5 on each criterion. Then summarize which story is better overall.
|
| 24 |
+
|
| 25 |
+
Story A: {story_a}
|
| 26 |
+
|
| 27 |
+
Story B: {story_b}
|
| 28 |
+
|
| 29 |
+
Respond in this format:
|
| 30 |
+
|
| 31 |
+
Story A:
|
| 32 |
+
- Coherence: ?/5
|
| 33 |
+
- Creativity: ?/5
|
| 34 |
+
- Engagement: ?/5
|
| 35 |
+
- Comment: ...
|
| 36 |
+
|
| 37 |
+
Story B:
|
| 38 |
+
- Coherence: ?/5
|
| 39 |
+
- Creativity: ?/5
|
| 40 |
+
- Engagement: ?/5
|
| 41 |
+
- Comment: ...
|
| 42 |
+
|
| 43 |
+
🟢 Overall Winner: Story A or Story B
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
# ✅ 평가 함수
|
| 47 |
+
def evaluate_stories(story_a, story_b):
|
| 48 |
+
prompt = build_prompt(story_a, story_b)
|
| 49 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024).to(device)
|
| 50 |
+
|
| 51 |
+
outputs = model.generate(
|
| 52 |
+
**inputs,
|
| 53 |
+
max_length=512,
|
| 54 |
+
temperature=0.7,
|
| 55 |
+
top_k=50,
|
| 56 |
+
top_p=0.95,
|
| 57 |
+
num_beams=4,
|
| 58 |
+
)
|
| 59 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 60 |
+
|
| 61 |
+
if len(result.strip()) < 20:
|
| 62 |
+
return "❌ 평가 실패: 응답이 너무 짧습니다. 스토리를 간단히 줄이거나 모델 성능을 높여보세요."
|
| 63 |
+
return result.strip()
|
| 64 |
+
|
| 65 |
+
# ✅ Gradio 인터페이스
|
| 66 |
+
with gr.Blocks() as demo:
|
| 67 |
+
gr.Markdown("## 🧙♂️ 동화 비교 평가기 (Flan-T5 Large)\n두 개의 동화를 비교해 평가합니다!")
|
| 68 |
+
|
| 69 |
+
with gr.Row():
|
| 70 |
+
story_a = gr.Textbox(label="📘 Story A", lines=12)
|
| 71 |
+
story_b = gr.Textbox(label="📗 Story B", lines=12)
|
| 72 |
+
|
| 73 |
+
result = gr.Textbox(label="📊 평가 결과", lines=20)
|
| 74 |
+
compare = gr.Button("🧠 평가하기")
|
| 75 |
+
|
| 76 |
+
compare.click(fn=evaluate_stories, inputs=[story_a, story_b], outputs=result)
|
| 77 |
+
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
demo.launch(share=True)
|