Update app.py
Browse files
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 |
-
|
| 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 |
-
|
| 52 |
-
|
| 53 |
-
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|