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