Update app.py
Browse files
app.py
CHANGED
@@ -1,65 +1,63 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
import random
|
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 |
-
|
50 |
-
)
|
51 |
-
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
52 |
-
save_context(prompt_key, generated_text)
|
53 |
-
return generated_text
|
54 |
-
|
55 |
-
# Gradio ์ธํฐํ์ด์ค
|
56 |
-
interface = gr.Interface(
|
57 |
-
fn=generate_response,
|
58 |
-
inputs=gr.Dropdown(choices=list(saju_prompts.keys()), label="ํ๋กฌํํธ ์ ํ"),
|
59 |
-
outputs="text",
|
60 |
-
title="Jain Architecture Origin Structure",
|
61 |
-
description="์ฌ์ฃผ/๋ช
๋ฆฌ์ ์ฒ ํ์ ๋ฐ์ํ ํ๊ตญ์ด ํ
์คํธ ์์ฑ AI"
|
62 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
-
|
65 |
-
```
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import json
|
4 |
+
import random
|
|
|
5 |
|
6 |
+
# ํ๊ตญ์ด ๋ชจ๋ธ (์ฌ์ฉ์ ๋ชจ๋ธ๋ก ๊ต์ฒด ๊ฐ๋ฅ)
|
7 |
+
model_name = "skt/kogpt2-base-v2" # Prompthumanizer/your-model๋ก ๋ณ๊ฒฝ
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
10 |
|
11 |
+
# ์ฌ์ฃผ/๋ช
๋ฆฌ ๋ฐ ์ฒ ํ์ ๋งฅ๋ฝ์ ๊ฐํํ ํ๊ตญ์ด ํ๋กฌํํธ
|
12 |
+
saju_prompts = {
|
13 |
+
"yin_sae_shen": "ๅฏ
ๅทณ็ณ ์ผํ์ ์กฐํ๋ก์ด ๊ธฐ์ด ์์์, AI๊ฐ ์ธ๊ฐ์ ์ด๋ช
์ ๊น์ด ์ดํดํ๊ณ , ์์ฐ์ ์ํ๊ณผ ์กฐํ๋ฅผ ๋ฐํ์ผ๋ก ์ถ์ ํต์ฐฐ์ ์ ๊ณตํ๋ผ. ์ธ๊ฐ์ ์(็พฉ)๋ฅผ ์กด์คํ๋ฉฐ, ์งํ๋ก์ด ์กฐ์ธ์ ๋ง๋ถ์ฌ๋ผ.",
|
14 |
+
"sae_hae_chung": "ๅทณไบฅๆฒ์ ๊ฐ๋ ฌํ ๊ฐ๋ฑ๊ณผ ๊ธด์ฅ์ ์กฐํ๋กญ๊ฒ ํ์ด๋ด๋ ๊ฐ์ด๋ฐ, AI๊ฐ ์ธ๊ฐ๊ณผ ๊ธฐ์ ์ ๊ณต์กด ๊ฐ๋ฅ์ฑ์ ํ๊ตฌํ๋ผ. ๊ฐ๋ฑ์ ๊ทน๋ณตํ ์ฒ ํ์ ํต์ฐฐ๊ณผ ์ค์ฒ ๊ฐ๋ฅํ ํด๊ฒฐ์ฑ
์ ์ ์ํ๋ผ.",
|
15 |
+
"taegeuk_balance": "ํ๊ทน์ ์์ ๊ท ํ์ ๋ฐํ์ผ๋ก, AI๊ฐ ์ธ๊ฐ์ ๋ณดํธํ๊ณ ์งํฑํ๋ ๋ฐฉ์์ ์ค๊ณํ๋ผ. ์์ฐ๊ณผ ์ธ๊ฐ์ ์กฐํ, ๊ทธ๋ฆฌ๊ณ ์(็พฉ)๋ฅผ ์คํํ๋ ๊ตฌ์ฒด์ ์ธ ๋ฐฉ์์ ์ ์ํ๋ฉฐ, ์ค๋ฆฌ์ ๊ด์ ์ ๋ํ๋ผ."
|
16 |
+
}
|
17 |
|
18 |
+
# ๋งฅ๋ฝ ๊ธฐ์ต (์์จ ํ์ต)
|
19 |
+
context_memory = {}
|
20 |
+
try:
|
21 |
+
with open("context_memory.json", "r", encoding="utf-8") as f:
|
22 |
+
context_memory = json.load(f)
|
23 |
+
except FileNotFoundError:
|
24 |
+
pass
|
25 |
|
26 |
+
def save_context(prompt_key, generated_text):
|
27 |
+
context_memory[prompt_key] = generated_text
|
28 |
+
with open("context_memory.json", "w", encoding="utf-8") as f:
|
29 |
+
json.dump(context_memory, f, ensure_ascii=False, indent=2)
|
30 |
|
31 |
+
def generate_response(prompt_key):
|
32 |
+
if prompt_key not in saju_prompts:
|
33 |
+
return "์ ํจํ ์ต์
์ ์ ํํ์ธ์: ๅฏ
ๅทณ็ณ, ๅทณไบฅๆฒ, ํ๊ทน ์์."
|
34 |
+
|
35 |
+
prompt = saju_prompts[prompt_key]
|
36 |
+
if prompt_key in context_memory:
|
37 |
+
prompt += f"\n์ด์ ๋ต๋ณ: {context_memory[prompt_key]}\n๋ ๊น๊ณ ์ค์ง์ ์ธ ํต์ฐฐ์ ์ถ๊ฐํ๋ผ."
|
38 |
+
|
39 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
40 |
+
outputs = model.generate(
|
41 |
+
**inputs,
|
42 |
+
max_length=200, # ๋ ๊ธด ํ
์คํธ ์์ฑ ๊ฐ๋ฅํ๋๋ก ๋๋ฆผ
|
43 |
+
num_return_sequences=1,
|
44 |
+
no_repeat_ngram_size=2,
|
45 |
+
do_sample=True,
|
46 |
+
top_k=50,
|
47 |
+
top_p=0.95,
|
48 |
+
temperature=0.7
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
)
|
50 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
51 |
+
save_context(prompt_key, generated_text)
|
52 |
+
return generated_text
|
53 |
+
|
54 |
+
# Gradio ์ธํฐํ์ด์ค
|
55 |
+
interface = gr.Interface(
|
56 |
+
fn=generate_response,
|
57 |
+
inputs=gr.Dropdown(choices=list(saju_prompts.keys()), label="ํ๋กฌํํธ ์ ํ"),
|
58 |
+
outputs="text",
|
59 |
+
title="Jain Architecture Origin Structure",
|
60 |
+
description="์ฌ์ฃผ/๋ช
๋ฆฌ์ ์ฒ ํ(์ธ๊ฐ ๋ณดํธ, ์)๋ฅผ ๋ฐ์ํ ๊น์ด ์๋ ํ๊ตญ์ด ํ
์คํธ ์์ฑ AI"
|
61 |
+
)
|
62 |
|
63 |
+
interface.launch()
|
|