devvtaco commited on
Commit
1fcd7be
Β·
verified Β·
1 Parent(s): dc5e80d

changed to openai

Browse files
Files changed (1) hide show
  1. app.py +39 -45
app.py CHANGED
@@ -1,53 +1,47 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
- import threading
4
-
5
- chat_model = None
6
- loading_done = False
7
- status_text = "⏳ λͺ¨λΈ λ‘œλ”© μ€‘μž…λ‹ˆλ‹€..."
8
-
9
- # λ°±κ·ΈλΌμš΄λ“œμ—μ„œ λͺ¨λΈ λ‘œλ”©
10
- def load_model_bg():
11
- global chat_model, loading_done, status_text
12
- chat_model = pipeline("text-generation", model="beomi/KoAlpaca-Polyglot-5.8B")
13
- loading_done = True
14
- status_text = "βœ… λͺ¨λΈ λ‘œλ”© μ™„λ£Œ! λ¬Έμž₯을 μž…λ ₯ν•΄λ³΄μ„Έμš”."
15
-
16
- # λŒ€λ‹΅ ν•¨μˆ˜
17
- def chat_with_model(message, history):
18
- global chat_model, loading_done
19
- if not loading_done:
20
- return history + [[message, "⚠️ λͺ¨λΈμ΄ 아직 λ‘œλ”© μ€‘μž…λ‹ˆλ‹€. μž μ‹œλ§Œ κΈ°λ‹€λ € μ£Όμ„Έμš”."]]
21
-
22
- prompt = f"""
23
- ### Instruction: λ‹€μŒ λ¬Έμž₯을 λΆ„μ„ν•΄μ„œ λ¬΄λ‘€ν•˜κ±°λ‚˜ 곡격적인 ν‘œν˜„μ΄ μžˆλŠ”μ§€ νŒλ‹¨ν•˜κ³ , μžˆλ‹€λ©΄ 더 예의 있게 고쳐쀘.
24
-
25
- ### Input:
26
- {message}
27
-
28
- ### Response:
29
- """
30
- response = chat_model(prompt, max_new_tokens=200)[0]['generated_text']
31
- response_only = response[len(prompt):].strip()
32
- return history + [[message, response_only]]
33
-
34
- # μƒνƒœ ν…μŠ€νŠΈ λ°˜ν™˜ ν•¨μˆ˜ (맀번 μƒˆλ‘œ μ½μ–΄μ˜΄)
35
- def get_status():
36
- return status_text
37
-
38
- # λ°±κ·ΈλΌμš΄λ“œμ—μ„œ λͺ¨λΈ λ‘œλ”© μ‹œμž‘
39
- threading.Thread(target=load_model_bg).start()
40
-
41
- # Gradio μ•±
42
  with gr.Blocks() as demo:
43
  chatbot = gr.Chatbot()
44
  msg = gr.Textbox(label="λ¬Έμž₯을 μž…λ ₯ν•˜μ„Έμš”", placeholder="예: λ„ˆ 정말 μ™œ κ·Έλ ‡κ²Œ 말해?")
45
- status = gr.Markdown(get_status)
46
 
47
  def respond_and_clear(user_input, history):
48
- updated_history = chat_with_model(user_input, history)
49
- return "", updated_history, get_status()
50
 
51
- msg.submit(respond_and_clear, [msg, chatbot], [msg, chatbot, status])
52
 
53
- demo.launch()
 
1
  import gradio as gr
2
+ import openai
3
+
4
+ # βœ… OpenAI API ν‚€ μž…λ ₯ (μ•ˆμ „ν•œ μ €μž₯ 방식 ꢌμž₯)
5
+ openai.api_key = "YOUR_OPENAI_API_KEY"
6
+
7
+ # GPTμ—κ²Œ 보낼 ν”„λ‘¬ν”„νŠΈλ₯Ό κ΅¬μ„±ν•˜κ³  응닡 λ°›κΈ°
8
+ def chat_with_gpt(message, history):
9
+ # μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈ: μ—­ν•  μ„€μ •
10
+ system_prompt = "λ„ˆλŠ” λ¬Έμž₯을 κ³΅μ†ν•˜κ³  예의 있게 λ°”κΏ”μ£ΌλŠ” ν•œκ΅­μ–΄ μ „λ¬Έκ°€μ•Ό."
11
+
12
+ # μ‚¬μš©μž ν”„λ‘¬ν”„νŠΈ ꡬ성
13
+ user_prompt = f"""μ•„λž˜ λ¬Έμž₯의 λ¬΄λ‘€ν•˜κ±°λ‚˜ 곡격적인 ν‘œν˜„μ„ μ°Ύμ•„λ‚΄κ³ , 더 예의 μžˆλŠ” ν‘œν˜„μœΌλ‘œ λ°”κΏ”μ€˜. κ°„λ‹¨ν•œ μ΄μœ λ„ ν•¨κ»˜ μ•Œλ €μ€˜.
14
+
15
+ λ¬Έμž₯: "{message}"
16
+
17
+ 응닡 ν˜•μ‹:
18
+ 1. 지적 사항: (λ¬΄λ‘€ν•œ ν‘œν˜„μ΄ μžˆλ‹€λ©΄ μ–΄λ–€ 뢀뢄인지 μ„€λͺ…)
19
+ 2. μ œμ•ˆ λ¬Έμž₯: (더 예의 있게 λ°”κΎΌ λ¬Έμž₯ μ œμ•ˆ)
20
+ """
21
+
22
+ try:
23
+ response = openai.ChatCompletion.create(
24
+ model="gpt-4", # λ˜λŠ” "gpt-3.5-turbo"
25
+ messages=[
26
+ {"role": "system", "content": system_prompt},
27
+ {"role": "user", "content": user_prompt}
28
+ ],
29
+ temperature=0.7,
30
+ )
31
+ reply = response["choices"][0]["message"]["content"].strip()
32
+ return history + [[message, reply]]
33
+ except Exception as e:
34
+ return history + [[message, f"⚠️ 였λ₯˜ λ°œμƒ: {str(e)}"]]
35
+
36
+ # Gradio μ•± ꡬ성
 
 
 
 
 
37
  with gr.Blocks() as demo:
38
  chatbot = gr.Chatbot()
39
  msg = gr.Textbox(label="λ¬Έμž₯을 μž…λ ₯ν•˜μ„Έμš”", placeholder="예: λ„ˆ 정말 μ™œ κ·Έλ ‡κ²Œ 말해?")
 
40
 
41
  def respond_and_clear(user_input, history):
42
+ updated_history = chat_with_gpt(user_input, history)
43
+ return "", updated_history
44
 
45
+ msg.submit(respond_and_clear, [msg, chatbot], [msg, chatbot])
46
 
47
+ demo.launch()