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ฑ ์ธก์ (Gradio GUI)
|
| 3 |
+
|
| 4 |
+
์ฌ์ฉ๋ฒ:
|
| 5 |
+
pip install gradio openai numpy nltk
|
| 6 |
+
python OpenAI_Originality_GUI.py
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import numpy as np
|
| 10 |
+
import gradio as gr
|
| 11 |
+
from openai import OpenAI
|
| 12 |
+
from nltk.tokenize import sent_tokenize, word_tokenize
|
| 13 |
+
import nltk
|
| 14 |
+
|
| 15 |
+
# NLTK ๋ฐ์ดํฐ ๋ค์ด๋ก๋
|
| 16 |
+
try:
|
| 17 |
+
nltk.data.find('tokenizers/punkt_tab')
|
| 18 |
+
except LookupError:
|
| 19 |
+
nltk.download('punkt_tab')
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def cosine_distance(v1, v2):
|
| 23 |
+
dot = np.dot(v1, v2)
|
| 24 |
+
norm = np.linalg.norm(v1) * np.linalg.norm(v2)
|
| 25 |
+
similarity = dot / norm if norm > 0 else 0
|
| 26 |
+
return 1 - similarity
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def get_embeddings(client, texts, model="text-embedding-3-large"):
|
| 30 |
+
response = client.embeddings.create(input=texts, model=model)
|
| 31 |
+
return [item.embedding for item in response.data]
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def calculate_sem_div(client, text):
|
| 35 |
+
sentences = sent_tokenize(text)
|
| 36 |
+
if len(sentences) < 2:
|
| 37 |
+
return 0.0, sentences
|
| 38 |
+
embeddings = get_embeddings(client, sentences)
|
| 39 |
+
distances = []
|
| 40 |
+
for i in range(len(sentences)):
|
| 41 |
+
for j in range(i):
|
| 42 |
+
dist = cosine_distance(embeddings[i], embeddings[j])
|
| 43 |
+
distances.append(dist)
|
| 44 |
+
return np.mean(distances), sentences
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def calculate_lex_div(text):
|
| 48 |
+
tokens = word_tokenize(text.lower())
|
| 49 |
+
tokens = [t for t in tokens if t.isalpha()]
|
| 50 |
+
if len(tokens) == 0:
|
| 51 |
+
return 0.0, 0, 0
|
| 52 |
+
unique_tokens = set(tokens)
|
| 53 |
+
return len(unique_tokens) / len(tokens), len(unique_tokens), len(tokens)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def analyze_originality(api_key, passage_a, passage_b):
|
| 57 |
+
if not api_key.strip():
|
| 58 |
+
return "Error: OpenAI API ํค๋ฅผ ์
๋ ฅํ์ธ์."
|
| 59 |
+
if not passage_a.strip() or not passage_b.strip():
|
| 60 |
+
return "Error: ๋ ๋จ๋ฝ ๋ชจ๋ ์
๋ ฅํ์ธ์."
|
| 61 |
+
|
| 62 |
+
try:
|
| 63 |
+
client = OpenAI(api_key=api_key.strip())
|
| 64 |
+
|
| 65 |
+
# Passage A ๋ถ์
|
| 66 |
+
sem_div_a, sentences_a = calculate_sem_div(client, passage_a)
|
| 67 |
+
lex_div_a, unique_a, total_a = calculate_lex_div(passage_a)
|
| 68 |
+
score_a = 0.50 * sem_div_a + 0.50 * lex_div_a
|
| 69 |
+
|
| 70 |
+
# Passage B ๋ถ์
|
| 71 |
+
sem_div_b, sentences_b = calculate_sem_div(client, passage_b)
|
| 72 |
+
lex_div_b, unique_b, total_b = calculate_lex_div(passage_b)
|
| 73 |
+
score_b = 0.50 * sem_div_b + 0.50 * lex_div_b
|
| 74 |
+
|
| 75 |
+
# ์ฐจ์ด ๊ณ์ฐ
|
| 76 |
+
diff = score_a - score_b
|
| 77 |
+
lower_score = min(score_a, score_b)
|
| 78 |
+
diff_percent = (abs(diff) / lower_score) * 100 if lower_score > 0 else 0
|
| 79 |
+
|
| 80 |
+
# ํ์
|
| 81 |
+
if diff_percent < 5:
|
| 82 |
+
judgment = "๋น์ทํจ"
|
| 83 |
+
elif diff_percent < 10:
|
| 84 |
+
judgment = "์ฐจ์ด ์์"
|
| 85 |
+
elif diff_percent < 15:
|
| 86 |
+
judgment = "์ ์๋ฏธํ ์ฐจ์ด"
|
| 87 |
+
else:
|
| 88 |
+
judgment = "ํ์คํ ์ฐจ์ด"
|
| 89 |
+
|
| 90 |
+
# ๊ฒฐ๊ณผ ํ
์คํธ
|
| 91 |
+
result = f"""
|
| 92 |
+
{'='*50}
|
| 93 |
+
๋ถ์ ๊ฒฐ๊ณผ
|
| 94 |
+
{'='*50}
|
| 95 |
+
|
| 96 |
+
{'ํญ๋ชฉ':<15} {'Passage A':>15} {'Passage B':>15}
|
| 97 |
+
{'-'*50}
|
| 98 |
+
{'๋ฌธ์ฅ ์':<15} {len(sentences_a):>15} {len(sentences_b):>15}
|
| 99 |
+
{'๊ณ ์ ๋จ์ด':<15} {unique_a:>15} {unique_b:>15}
|
| 100 |
+
{'์ ์ฒด ๋จ์ด':<15} {total_a:>15} {total_b:>15}
|
| 101 |
+
{'-'*50}
|
| 102 |
+
{'sem_div':<15} {sem_div_a:>15.4f} {sem_div_b:>15.4f}
|
| 103 |
+
{'lex_div':<15} {lex_div_a:>15.4f} {lex_div_b:>15.4f}
|
| 104 |
+
{'๋
์ฐฝ์ฑ ์ ์':<15} {score_a:>15.4f} {score_b:>15.4f}
|
| 105 |
+
|
| 106 |
+
{'='*50}
|
| 107 |
+
์ฐจ์ด ๋น์จ
|
| 108 |
+
{'='*50}
|
| 109 |
+
์ ์ ์ฐจ์ด: {abs(diff):.4f}
|
| 110 |
+
์ฐจ์ด ๋น์จ: {diff_percent:.1f}%
|
| 111 |
+
ํ์ : {judgment}
|
| 112 |
+
|
| 113 |
+
{'='*50}
|
| 114 |
+
์ต์ข
ํ์
|
| 115 |
+
{'='*50}
|
| 116 |
+
"""
|
| 117 |
+
if diff_percent < 5:
|
| 118 |
+
result += f"๋ ํ
์คํธ์ ๋
์ฐฝ์ฑ์ ๋น์ทํจ (์ฐจ์ด {diff_percent:.1f}%)"
|
| 119 |
+
elif diff > 0:
|
| 120 |
+
result += f"Passage A๊ฐ ๋ ๋
์ฐฝ์ (์ฐจ์ด {diff_percent:.1f}%, {judgment})"
|
| 121 |
+
else:
|
| 122 |
+
result += f"Passage B๊ฐ ๋ ๋
์ฐฝ์ (์ฐจ์ด {diff_percent:.1f}%, {judgment})"
|
| 123 |
+
|
| 124 |
+
return result
|
| 125 |
+
|
| 126 |
+
except Exception as e:
|
| 127 |
+
return f"Error: {str(e)}"
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
# Gradio ์ธํฐํ์ด์ค
|
| 131 |
+
demo = gr.Interface(
|
| 132 |
+
fn=analyze_originality,
|
| 133 |
+
inputs=[
|
| 134 |
+
gr.Textbox(label="OpenAI API Key", type="password", placeholder="sk-..."),
|
| 135 |
+
gr.Textbox(label="Passage A", lines=8, placeholder="์ฒซ ๋ฒ์งธ ๋จ๋ฝ ์
๋ ฅ..."),
|
| 136 |
+
gr.Textbox(label="Passage B", lines=8, placeholder="๋ ๋ฒ์งธ ๋จ๋ฝ ์
๋ ฅ...")
|
| 137 |
+
],
|
| 138 |
+
outputs=gr.Textbox(label="๋ถ์ ๊ฒฐ๊ณผ", lines=25),
|
| 139 |
+
title="OpenAI Embedding ๋
์ฐฝ์ฑ ๋ถ์",
|
| 140 |
+
description="๋ ๋จ๋ฝ์ ๋
์ฐฝ์ฑ์ ๋น๊ตํฉ๋๋ค. (sem_div 50% + lex_div 50%)",
|
| 141 |
+
flagging_mode="never"
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
if __name__ == "__main__":
|
| 145 |
+
demo.launch()
|
OpenAI_README.txt
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# OpenAI Embedding Originality Analyzer
|
| 2 |
+
|
| 3 |
+
Compare the originality of two text passages using OpenAI's text-embedding-3-large model.
|
| 4 |
+
|
| 5 |
+
## Metrics
|
| 6 |
+
|
| 7 |
+
- **sem_div (50%)**: Semantic diversity - mean cosine distance between sentence embeddings
|
| 8 |
+
- **lex_div (50%)**: Lexical diversity - unique tokens / total tokens
|
| 9 |
+
- **Originality Score**: Weighted sum of sem_div and lex_div
|
| 10 |
+
|
| 11 |
+
## Requirements
|
| 12 |
+
|
| 13 |
+
- Python 3.8+
|
| 14 |
+
- OpenAI API Key (https://platform.openai.com/api-keys)
|
| 15 |
+
|
| 16 |
+
## Installation
|
| 17 |
+
|
| 18 |
+
In command prompt:
|
| 19 |
+
|
| 20 |
+
```
|
| 21 |
+
pip install gradio openai numpy nltk
|
| 22 |
+
```
|
| 23 |
+
|
| 24 |
+
## Usage
|
| 25 |
+
|
| 26 |
+
1. Download `OpenAI_Originality_GUI.py`
|
| 27 |
+
|
| 28 |
+
2. Run in command prompt:
|
| 29 |
+
```
|
| 30 |
+
python OpenAI_Originality_GUI.py
|
| 31 |
+
```
|
| 32 |
+
|
| 33 |
+
3. Browser opens automatically (or go to http://127.0.0.1:7860)
|
| 34 |
+
|
| 35 |
+
4. Enter:
|
| 36 |
+
- OpenAI API Key (sk-...)
|
| 37 |
+
- Passage A (first paragraph)
|
| 38 |
+
- Passage B (second paragraph)
|
| 39 |
+
|
| 40 |
+
5. Click Submit
|
| 41 |
+
|
| 42 |
+
6. View results
|
| 43 |
+
|
| 44 |
+
## Interpretation
|
| 45 |
+
|
| 46 |
+
| Difference | Judgment |
|
| 47 |
+
|------------|----------|
|
| 48 |
+
| < 5% | Similar |
|
| 49 |
+
| 5% - 10% | Some difference |
|
| 50 |
+
| 10% - 15% | Significant difference |
|
| 51 |
+
| > 15% | Clear difference |
|
| 52 |
+
|
| 53 |
+
## Model Information
|
| 54 |
+
|
| 55 |
+
- Model: text-embedding-3-large
|
| 56 |
+
- Embedding dimension: 3072
|
| 57 |
+
- Provider: OpenAI
|
| 58 |
+
|
| 59 |
+
## Notes
|
| 60 |
+
|
| 61 |
+
- API costs apply (check OpenAI pricing)
|
| 62 |
+
- Threshold values are empirically derived from pilot testing
|
| 63 |
+
- Scores are relative comparisons, not absolute measures
|
| 64 |
+
|
| 65 |
+
## License
|
| 66 |
+
|
| 67 |
+
Apache-2.0
|
| 68 |
+
|
| 69 |
+
## Citation
|
| 70 |
+
|
| 71 |
+
If you use this tool, please acknowledge:
|
| 72 |
+
- OpenAI text-embedding-3-large model
|
| 73 |
+
- NLTK for tokenization
|