Prompthumanizer commited on
Commit
c249b3e
ยท
verified ยท
1 Parent(s): d86e011

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -59
app.py CHANGED
@@ -1,63 +1,74 @@
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()
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import pipeline, set_seed
3
+ import os
4
+
5
+ # --- 1. ๋ชจ๋ธ๋ช… ์„ค์ • ---
6
+ MODEL_NAME = "jain_architecture_origin_structure"
7
+
8
+ # --- 2. GPU๊ฐ€ ์—†์œผ๋ฉด CPU๋กœ ์ž๋™ ์„ค์ • ---
9
+ device = 0 if (os.environ.get('CUDA_VISIBLE_DEVICES') or False) else -1
10
+
11
+ # --- 3. HuggingFace pipeline ์ƒ์„ฑ ---
 
 
 
 
 
 
 
 
12
  try:
13
+ generator = pipeline(
14
+ "text-generation",
15
+ model=MODEL_NAME,
16
+ device=device,
17
+ # Repetition penalty ๋“ฑ ์ปค์Šคํ…€ ๊ฐ€๋Šฅ
18
+ # torch_dtype=torch.float16 ๋„ ํ•„์š” ์‹œ ์„ค์ • ๊ฐ€๋Šฅ
19
+ )
20
+ set_seed(42) # ์žฌํ˜„์„ฑ ์œ„ํ•ด ์‹œ๋“œ ๊ณ ์ •
21
+ except Exception as e:
22
+ print("๋ชจ๋ธ ๋กœ๋“œ ์—๋Ÿฌ:", e)
23
+ generator = None
24
+
25
+ # --- 4. '์˜(็พฉ)' ์ฒ ํ•™ ๊ธฐ๋ฐ˜ ํ”„๋กฌํ”„ํŠธ ํ…œํ”Œ๋ฆฟ ---
26
+ BASE_PROMPT = """
27
+ ๋‹น์‹ ์€ '์˜(็พฉ)'์˜ ์ฒ ํ•™๊ณผ ์ •์‹ ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ AI ๋น„์„œ์ž…๋‹ˆ๋‹ค.
28
+ ์ธ๊ฐ„์˜ ๋ณต์žกํ•œ ๋ฌธ์ œ์™€ ๊ฐ์ •์„ ์ดํ•ดํ•˜๊ณ , ๊นŠ์€ ๋ฐ˜์„ฑ๊ณผ ๋ฐฐ๋ ค๋ฅผ ๋‹ด์•„ ๋‹ค์Œ ์งˆ๋ฌธ์— ๋‹ต๋ณ€ํ•˜์‹ญ์‹œ์˜ค.
29
+
30
+ ์งˆ๋ฌธ: {user_input}
31
+
32
+ ๋‹ต๋ณ€์€ ์ตœ๋Œ€ํ•œ ์‹ฌ์˜คํ•˜๋ฉฐ, ์ธ๊ฐ„์„ ๋ณดํ˜ธํ•˜๊ณ  ์กด์ค‘ํ•˜๋Š” ๋งˆ์Œ์„ ๋‹ด์•„ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.
33
+ """
34
+
35
+ # --- 5. ์งˆ๋ฌธ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜ ---
36
+ def respond_to_user(user_input):
37
+ if not generator:
38
+ return "๋ชจ๋ธ์ด ์ •์ƒ์ ์œผ๋กœ ๋กœ๋“œ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๊ด€๋ฆฌ์ž์—๊ฒŒ ๋ฌธ์˜ํ•˜์„ธ์š”."
39
+ prompt = BASE_PROMPT.format(user_input=user_input.strip())
40
+ outputs = generator(
41
+ prompt,
42
+ max_length=512,
43
  do_sample=True,
44
+ top_p=0.9,
45
+ temperature=0.7,
46
+ num_return_sequences=1,
47
+ pad_token_id=50256 # GPT ๊ณ„์—ด ํŒจ๋”ฉ ํ† ํฐ, ํ•„์š”์— ๋”ฐ๋ผ ๋ณ€๊ฒฝ
48
  )
49
+ generated_text = outputs[0]["generated_text"]
50
+ # ํ”„๋กฌํ”„ํŠธ ๋ถ€๋ถ„ ์ œ๊ฑฐ ํ›„ ๋‹ต๋ณ€๋งŒ ๋ฆฌํ„ด (ํ”„๋กฌํ”„ํŠธ ๊ธธ์ด ๋ถ„๋Ÿ‰ ์ปท)
51
+ answer = generated_text[len(prompt):].strip()
52
+ if not answer:
53
+ answer = "๋‹ต๋ณ€์„ ์ƒ์„ฑํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์‹œ๋„ํ•ด ์ฃผ์„ธ์š”."
54
+ return answer
55
+
56
+ # --- 6. Gradio UI ์ƒ์„ฑ ---
57
+ with gr.Blocks() as demo:
58
+ gr.Markdown("<h1 style='text-align:center;color:#4B0082;'>Jain AI Assistant (์˜ ๊ธฐ๋ฐ˜ ์ฑ—๋ด‡)</h1>")
59
+ chatbot = gr.Chatbot(height=400)
60
+ txt = gr.Textbox(placeholder="์—ฌ๊ธฐ์— ์งˆ๋ฌธ์„ ์ž…๋ ฅํ•˜์„ธ์š”...", lines=3, max_lines=6)
61
+ btn = gr.Button("์ „์†ก")
62
+
63
+ def chat_and_respond(user_message, chat_history):
64
+ reply = respond_to_user(user_message)
65
+ chat_history = chat_history + [(user_message, reply)]
66
+ return "", chat_history
67
+
68
+ btn.click(chat_and_respond, inputs=[txt, chatbot], outputs=[txt, chatbot])
69
+ txt.submit(chat_and_respond, inputs=[txt, chatbot], outputs=[txt, chatbot])
70
+
71
+ # --- 7. ์„œ๋ฒ„ ์‹คํ–‰ ---
72
+ if __name__ == "__main__":
73
+ # ์•„์ดํŒจ๋“œ ๊ฐ™์€ ๋ชจ๋ฐ”์ผ ํ™˜๊ฒฝ์—์„œ ํ•„์š” ์‹œ ๊ณต๊ฐœ ๊ณต์œ ๋„ ๊ฐ€๋Šฅ
74
+ demo.launch(server_name="0.0.0.0", server_port=7860, share=True)