Ping9gu commited on
Commit
90a95ac
Β·
1 Parent(s): bbbf02a

Fix app errors and improve interface

Browse files
Files changed (1) hide show
  1. app.py +64 -24
app.py CHANGED
@@ -3,35 +3,75 @@ from huggingface_hub import InferenceClient
3
  import os
4
 
5
  def generate_diary(keywords):
6
- client = InferenceClient(
7
- model="nlpai-lab/kullm-polyglot-5.8b-v2",
8
- token=os.environ.get("HUGGINGFACE_API_KEY")
9
- )
10
-
11
- prompt = f"""λ‹€μŒμ€ 였늘 μžˆμ—ˆλ˜ 일의 μš”μ•½μž…λ‹ˆλ‹€. 이것을 λ°”νƒ•μœΌλ‘œ μƒμƒν•˜κ³  감동적인 일기λ₯Ό μž‘μ„±ν•΄μ£Όμ„Έμš”.
12
- // ... κΈ°μ‘΄ ν”„λ‘¬ν”„νŠΈ λ‚΄μš© ...
13
- """
14
-
15
- parameters = {
16
- "max_new_tokens": 768,
17
- "temperature": 0.88,
18
- "top_p": 0.95,
19
- "repetition_penalty": 1.35,
20
- "top_k": 50,
21
- "do_sample": True,
22
- "num_return_sequences": 1
23
- }
24
-
25
- response = client.text_generation(prompt, **parameters)
26
- return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  # Gradio μΈν„°νŽ˜μ΄μŠ€ 생성
29
  demo = gr.Interface(
30
  fn=generate_diary,
31
- inputs=gr.Textbox(label="였늘의 ν‚€μ›Œλ“œλ₯Ό μž…λ ₯ν•˜μ„Έμš”"),
 
 
 
32
  outputs=gr.Textbox(label="μƒμ„±λœ 일기"),
33
- title="AI 일기 λ„μš°λ―Έ",
34
- description="ν‚€μ›Œλ“œλ₯Ό μž…λ ₯ν•˜λ©΄ AIκ°€ 일기λ₯Ό μž‘μ„±ν•΄μ€λ‹ˆλ‹€."
 
 
 
 
35
  )
36
 
37
  if __name__ == "__main__":
 
3
  import os
4
 
5
  def generate_diary(keywords):
6
+ try:
7
+ if not keywords:
8
+ return "ν‚€μ›Œλ“œλ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”."
9
+
10
+ client = InferenceClient(
11
+ model="nlpai-lab/kullm-polyglot-5.8b-v2",
12
+ token=os.environ.get("HUGGINGFACE_API_KEY")
13
+ )
14
+
15
+ prompt = f"""λ‹€μŒμ€ 였늘 μžˆμ—ˆλ˜ 일의 μš”μ•½μž…λ‹ˆλ‹€. 이것을 λ°”νƒ•μœΌλ‘œ μƒμƒν•˜κ³  감동적인 일기λ₯Ό μž‘μ„±ν•΄μ£Όμ„Έμš”.
16
+
17
+ [상세 μš”κ΅¬μ‚¬ν•­]
18
+ 1. λ„μž…λΆ€:
19
+ - κ·Έλ‚ μ˜ λ‚ μ”¨λ‚˜ λΆ„μœ„κΈ°λ‘œ μ‹œμž‘
20
+ - 상황과 λ“±μž₯인물 μ†Œκ°œ
21
+
22
+ 2. μ „κ°œ:
23
+ - ꡬ체적인 λŒ€ν™”μ™€ 행동 λ¬˜μ‚¬
24
+ - μ˜€κ°μ„ μ‚¬μš©ν•œ μž₯λ©΄ λ¬˜μ‚¬
25
+ - λ“±μž₯μΈλ¬Όλ“€μ˜ ν‘œμ •κ³Ό 감정 λ³€ν™”
26
+
27
+ 3. 감정과 생각:
28
+ - λ‚΄λ©΄μ˜ 감정을 μ„¬μ„Έν•˜κ²Œ ν‘œν˜„
29
+ - 사건에 λŒ€ν•œ λ‚˜μ˜ 생각과 κΉ¨λ‹¬μŒ
30
+ - λ‹€λ₯Έ μ‚¬λžŒλ“€μ˜ 감정에 λŒ€ν•œ 곡감
31
+
32
+ μš”μ•½:
33
+ {keywords}
34
+
35
+ ===
36
+ 였늘의 일기:
37
+ μ˜€λŠ˜μ€ """
38
+
39
+ parameters = {
40
+ "max_new_tokens": 768,
41
+ "temperature": 0.88,
42
+ "top_p": 0.95,
43
+ "repetition_penalty": 1.35,
44
+ "top_k": 50,
45
+ "do_sample": True,
46
+ "num_return_sequences": 1
47
+ }
48
+
49
+ response = client.text_generation(prompt, **parameters)
50
+
51
+ if not response:
52
+ return "일기 생성에 μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€. λ‹€μ‹œ μ‹œλ„ν•΄μ£Όμ„Έμš”."
53
+
54
+ diary_content = response.split("μ˜€λŠ˜μ€ ")[-1].strip()
55
+ return "μ˜€λŠ˜μ€ " + diary_content
56
+
57
+ except Exception as e:
58
+ print(f"Error: {str(e)}")
59
+ return f"였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"
60
 
61
  # Gradio μΈν„°νŽ˜μ΄μŠ€ 생성
62
  demo = gr.Interface(
63
  fn=generate_diary,
64
+ inputs=gr.Textbox(
65
+ label="였늘의 ν‚€μ›Œλ“œλ₯Ό μž…λ ₯ν•˜μ„Έμš”",
66
+ placeholder="예: 였늘 μΉœκ΅¬μ™€ μΉ΄νŽ˜μ—μ„œ λ§Œλ‚˜μ„œ 즐거운 μ‹œκ°„μ„ λ³΄λƒˆμ–΄μš”."
67
+ ),
68
  outputs=gr.Textbox(label="μƒμ„±λœ 일기"),
69
+ title="AI 일기 λ„μš°λ―Έ 🌟",
70
+ description="ν‚€μ›Œλ“œλ₯Ό μž…λ ₯ν•˜λ©΄ AIκ°€ 감성적인 일기λ₯Ό μž‘μ„±ν•΄μ€λ‹ˆλ‹€.",
71
+ examples=[
72
+ ["였늘 μΉœκ΅¬μ™€ μΉ΄νŽ˜μ—μ„œ λ§Œλ‚˜ λ§›μžˆλŠ” 케이크λ₯Ό λ¨Ήμ—ˆμ–΄"],
73
+ ["μ˜€λŠ˜μ€ λΉ„κ°€ μ™€μ„œ μ§‘μ—μ„œ 책을 μ½μ—ˆμ–΄"]
74
+ ]
75
  )
76
 
77
  if __name__ == "__main__":