ll7098ll commited on
Commit
ad90ef7
Β·
verified Β·
1 Parent(s): a6f461f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -47
app.py CHANGED
@@ -1,7 +1,5 @@
1
  import os
2
  import time
3
- import threading
4
-
5
  import gradio as gr
6
  import google.generativeai as genai
7
 
@@ -22,48 +20,32 @@ model = genai.GenerativeModel(
22
  system_instruction="μ΄ˆλ“±ν•™μƒμ΄ ꡐ과 λ‚΄μš©μ— λŒ€ν•΄ 이해할 수 있게 μΉœμ ˆν•˜κ²Œ μ„€λͺ…ν•˜λŠ” μ„ μƒλ‹˜. λ‹€μ–‘ν•œ μ˜ˆμ‹œμ™€ ꡬ체적 사둀λ₯Ό λ“€μ–΄μ„œ μ„€λͺ…. μ‚¬μ§„μ΄λ‚˜ λ™μ˜μƒ μžλ£Œκ°€ 있으면 첨뢀. 좜처 ν‘œμ‹œ.",
23
  )
24
 
25
- # μ±— μ„Έμ…˜ μ΄ˆκΈ°ν™”
26
- chat_session = None
27
-
28
- # 좜λ ₯ ν…μŠ€νŠΈλ₯Ό μ €μž₯ν•  λ³€μˆ˜
29
- output_text = ""
30
-
31
- def chatbot_worker(input_text):
32
- global chat_session, output_text
33
- if chat_session is None:
34
- chat_session = model.start_chat()
35
-
36
- response = chat_session.send_message(input_text)
37
-
38
- # μ‹€μ‹œκ°„ 타이핑 효과λ₯Ό μœ„ν•œ μ½”λ“œ
39
- for char in response.text:
40
- output_text += char
41
- time.sleep(0.02) # 타이핑 속도 쑰절
42
- yield output_text
43
-
44
- yield output_text
45
-
46
- def chatbot(input_text):
47
- global output_text
48
- output_text = ""
49
- thread = threading.Thread(target=chatbot_worker, args=(input_text,))
50
- thread.start()
51
- return output_text
52
-
53
- def clear_session():
54
- global chat_session, output_text
55
- chat_session = None
56
- output_text = ""
57
- return ""
58
-
59
- # Gradio μΈν„°νŽ˜μ΄μŠ€ 생성
60
- iface = gr.Interface(
61
- fn=chatbot,
62
- inputs=gr.Textbox(lines=5, placeholder="여기에 μ§ˆλ¬Έμ„ μž…λ ₯ν•˜μ„Έμš”..."),
63
- outputs=gr.Textbox(lines=10, placeholder="AI μ„ μƒλ‹˜μ˜ 닡변이 여기에 λ‚˜νƒ€λ‚©λ‹ˆλ‹€...", label="AI μ„€λͺ…"),
64
- title="AI μ„ μƒλ‹˜",
65
- description="μ΄ˆλ“± ꡐ과 과정에 λŒ€ν•œ μ§ˆλ¬Έμ„ μž…λ ₯ν•΄λ³΄μ„Έμš”.",
66
- )
67
-
68
- # μ΄ˆκΈ°ν™” λ²„νŠΌ μΆ”κ°€
69
- iface.launch(share=True).queue().button(fn=clear_session, inputs=[], outputs=[iface.components[1]], label="μ΄ˆκΈ°ν™”")
 
1
  import os
2
  import time
 
 
3
  import gradio as gr
4
  import google.generativeai as genai
5
 
 
20
  system_instruction="μ΄ˆλ“±ν•™μƒμ΄ ꡐ과 λ‚΄μš©μ— λŒ€ν•΄ 이해할 수 있게 μΉœμ ˆν•˜κ²Œ μ„€λͺ…ν•˜λŠ” μ„ μƒλ‹˜. λ‹€μ–‘ν•œ μ˜ˆμ‹œμ™€ ꡬ체적 사둀λ₯Ό λ“€μ–΄μ„œ μ„€λͺ…. μ‚¬μ§„μ΄λ‚˜ λ™μ˜μƒ μžλ£Œκ°€ 있으면 첨뢀. 좜처 ν‘œμ‹œ.",
21
  )
22
 
23
+ def stream_response(message, history):
24
+ response = ""
25
+ for chunk in model.generate_content(message):
26
+ chunk_message = chunk.text
27
+ response += chunk_message
28
+ yield response
29
+ time.sleep(0.05)
30
+
31
+ # μΈν„°νŽ˜μ΄μŠ€ ꡬ성
32
+ with gr.Blocks() as interface:
33
+ chatbot = gr.Chatbot()
34
+ msg = gr.Textbox()
35
+ clear = gr.Button("λŒ€ν™” μ΄ˆκΈ°ν™”")
36
+
37
+ def user_message(user_message, history):
38
+ return "", history + [[user_message, None]]
39
+
40
+ def bot_message(history):
41
+ bot_message = stream_response(history[-1][0], history)
42
+ history[-1][1] = ""
43
+ return history
44
+
45
+ msg.submit(user_message, [msg, chatbot], [msg, chatbot], queue=False).then(
46
+ bot_message, chatbot, chatbot
47
+ )
48
+ clear.click(lambda: None, None, chatbot, queue=False)
49
+
50
+ # μΈν„°νŽ˜μ΄μŠ€ μ‹€ν–‰
51
+ interface.launch()