taldemir commited on
Commit
a2d5f05
ยท
verified ยท
1 Parent(s): 7afcaab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -49
app.py CHANGED
@@ -2,13 +2,10 @@ import os
2
  import gradio as gr
3
  from dotenv import load_dotenv
4
  from openai import OpenAI
5
- from fastapi import FastAPI
6
- import threading
7
- import uvicorn
8
  from prompts.initial_prompt import INITIAL_PROMPT
9
  from prompts.main_prompt import MAIN_PROMPT
10
 
11
- # โœ… Load OpenAI API Key
12
  if os.path.exists(".env"):
13
  load_dotenv(".env")
14
 
@@ -16,36 +13,33 @@ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
16
 
17
  client = OpenAI(api_key=OPENAI_API_KEY)
18
 
19
- # โœ… FastAPI App for Serving Prompts
20
- fastapi_app = FastAPI()
21
 
22
- @fastapi_app.get("/initial_prompt")
23
- async def get_initial_prompt():
24
- return {"prompt": INITIAL_PROMPT}
25
 
26
- @fastapi_app.get("/main_prompt")
27
- async def get_main_prompt():
28
- return {"prompt": MAIN_PROMPT}
29
-
30
- # โœ… Chatbot Function
31
  def gpt_call(history, user_message,
32
  model="gpt-4o-mini",
33
  max_tokens=512,
34
  temperature=0.7,
35
  top_p=0.95):
36
  """
37
- OpenAI ChatCompletion API call
 
 
38
  """
 
39
  messages = [{"role": "system", "content": MAIN_PROMPT}]
40
 
 
 
41
  for user_text, assistant_text in history:
42
  if user_text:
43
  messages.append({"role": "user", "content": user_text})
44
  if assistant_text:
45
  messages.append({"role": "assistant", "content": assistant_text})
46
 
 
47
  messages.append({"role": "user", "content": user_message})
48
 
 
49
  completion = client.chat.completions.create(
50
  model=model,
51
  messages=messages,
@@ -55,47 +49,61 @@ def gpt_call(history, user_message,
55
  )
56
  return completion.choices[0].message.content
57
 
58
- # โœ… Gradio Chatbot UI
59
  def respond(user_message, history):
 
 
 
 
 
 
60
  if not user_message:
61
  return "", history
62
 
 
63
  assistant_reply = gpt_call(history, user_message)
 
 
64
  history.append((user_message, assistant_reply))
65
 
 
66
  return "", history
67
 
68
- # โœ… Gradio UI
69
- def launch_gradio():
70
- with gr.Blocks() as gradio_app:
71
- gr.Markdown("## Simple Chat Interface")
72
-
73
- chatbot = gr.Chatbot(
74
- value=[{"role": "assistant", "content": INITIAL_PROMPT}],
75
- height=500,
76
- type="messages"
77
- )
78
-
79
- state_history = gr.State([("", INITIAL_PROMPT)])
80
-
81
- user_input = gr.Textbox(
82
- placeholder="Type your message here...",
83
- label="Your Input"
84
- )
85
-
86
- user_input.submit(
87
- respond,
88
- inputs=[user_input, state_history],
89
- outputs=[user_input, chatbot]
90
- ).then(
91
- fn=lambda _, h: h,
92
- inputs=[user_input, chatbot],
93
- outputs=[state_history]
94
- )
95
-
96
- gradio_app.launch(server_name="0.0.0.0", server_port=7860, show_error=True, enable_queue=True)
97
-
98
- # โœ… Run FastAPI and Gradio Separately
 
 
 
 
 
 
99
  if __name__ == "__main__":
100
- threading.Thread(target=launch_gradio, daemon=True).start()
101
- uvicorn.run(fastapi_app, host="0.0.0.0", port=8000)
 
2
  import gradio as gr
3
  from dotenv import load_dotenv
4
  from openai import OpenAI
 
 
 
5
  from prompts.initial_prompt import INITIAL_PROMPT
6
  from prompts.main_prompt import MAIN_PROMPT
7
 
8
+ # .env ํŒŒ์ผ์—์„œ OPENAI_API_KEY ๋กœ๋“œ
9
  if os.path.exists(".env"):
10
  load_dotenv(".env")
11
 
 
13
 
14
  client = OpenAI(api_key=OPENAI_API_KEY)
15
 
 
 
16
 
 
 
 
17
 
 
 
 
 
 
18
  def gpt_call(history, user_message,
19
  model="gpt-4o-mini",
20
  max_tokens=512,
21
  temperature=0.7,
22
  top_p=0.95):
23
  """
24
+ OpenAI ChatCompletion API๋ฅผ ํ†ตํ•ด ๋‹ต๋ณ€์„ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜.
25
+ - history: [(user_text, assistant_text), ...]
26
+ - user_message: ์‚ฌ์šฉ์ž๊ฐ€ ๋ฐฉ๊ธˆ ์ž…๋ ฅํ•œ ๋ฉ”์‹œ์ง€
27
  """
28
+ # 1) ์‹œ์Šคํ…œ ๋ฉ”์‹œ์ง€(=MAIN_PROMPT)๋ฅผ ๊ฐ€์žฅ ์•ž์— ์ถ”๊ฐ€
29
  messages = [{"role": "system", "content": MAIN_PROMPT}]
30
 
31
+ # 2) ๊ธฐ์กด ๋Œ€ํ™” ๊ธฐ๋ก(history)์„ OpenAI ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜
32
+ # user_text -> 'user' / assistant_text -> 'assistant'
33
  for user_text, assistant_text in history:
34
  if user_text:
35
  messages.append({"role": "user", "content": user_text})
36
  if assistant_text:
37
  messages.append({"role": "assistant", "content": assistant_text})
38
 
39
+ # 3) ๋งˆ์ง€๋ง‰์— ์ด๋ฒˆ ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์„ ์ถ”๊ฐ€
40
  messages.append({"role": "user", "content": user_message})
41
 
42
+ # 4) OpenAI API ํ˜ธ์ถœ
43
  completion = client.chat.completions.create(
44
  model=model,
45
  messages=messages,
 
49
  )
50
  return completion.choices[0].message.content
51
 
 
52
  def respond(user_message, history):
53
+ """
54
+ Gradio ์ƒ์—์„œ submitํ•  ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜
55
+ - user_message: ์‚ฌ์šฉ์ž๊ฐ€ ๋ฐฉ๊ธˆ ์นœ ๋ฉ”์‹œ์ง€
56
+ - history: ๊ธฐ์กด (user, assistant) ํŠœํ”Œ ๋ฆฌ์ŠคํŠธ
57
+ """
58
+ # ์‚ฌ์šฉ์ž๊ฐ€ ๋นˆ ๋ฌธ์ž์—ด์„ ๋ณด๋ƒˆ๋‹ค๋ฉด ์•„๋ฌด ์ผ๋„ ํ•˜์ง€ ์•Š์Œ
59
  if not user_message:
60
  return "", history
61
 
62
+ # GPT ๋ชจ๋ธ๋กœ๋ถ€ํ„ฐ ์‘๋‹ต์„ ๋ฐ›์Œ
63
  assistant_reply = gpt_call(history, user_message)
64
+
65
+ # history์— (user, assistant) ์Œ ์ถ”๊ฐ€
66
  history.append((user_message, assistant_reply))
67
 
68
+ # Gradio์—์„œ๋Š” (์ƒˆ๋กœ ๋น„์›Œ์งˆ ์ž…๋ ฅ์ฐฝ, ๊ฐฑ์‹ ๋œ history)๋ฅผ ๋ฐ˜ํ™˜
69
  return "", history
70
 
71
+ ##############################
72
+ # Gradio Blocks UI
73
+ ##############################
74
+ with gr.Blocks() as demo:
75
+ gr.Markdown("## Simple Chat Interface")
76
+
77
+ # Chatbot ์ดˆ๊ธฐ ์ƒํƒœ๋ฅผ ์„ค์ •
78
+ # ์ฒซ ๋ฒˆ์งธ ๋ฉ”์‹œ์ง€๋Š” (user="", assistant=INITIAL_PROMPT) ํ˜•ํƒœ๋กœ ๋„ฃ์–ด
79
+ # ํ™”๋ฉด์ƒ์—์„œ 'assistant'๊ฐ€ INITIAL_PROMPT๋ฅผ ๋งํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด๊ฒŒ ํ•จ
80
+ chatbot = gr.Chatbot(
81
+ value=[("", INITIAL_PROMPT)], # (user, assistant)
82
+ height=500
83
+ )
84
+
85
+ # (user, assistant) ์Œ์„ ์ €์žฅํ•  ํžˆ์Šคํ† ๋ฆฌ ์ƒํƒœ
86
+ # ์—ฌ๊ธฐ์„œ๋„ ๋™์ผํ•œ ์ดˆ๊ธฐ ์ƒํƒœ๋ฅผ ๋„ฃ์–ด์คŒ
87
+ state_history = gr.State([("", INITIAL_PROMPT)])
88
+
89
+ # ์‚ฌ์šฉ์ž ์ž…๋ ฅ
90
+ user_input = gr.Textbox(
91
+ placeholder="Type your message here...",
92
+ label="Your Input"
93
+ )
94
+
95
+ # ์ž…๋ ฅ์ด submit๋˜๋ฉด respond() ํ˜ธ์ถœ โ†’ ์ถœ๋ ฅ์€ (์ƒˆ ์ž…๋ ฅ์ฐฝ, ๊ฐฑ์‹ ๋œ chatbot)
96
+ user_input.submit(
97
+ respond,
98
+ inputs=[user_input, state_history],
99
+ outputs=[user_input, chatbot]
100
+ ).then(
101
+ # respond ๋๋‚œ ๋’ค, ์ตœ์‹  history๋ฅผ state_history์— ๋ฐ˜์˜
102
+ fn=lambda _, h: h,
103
+ inputs=[user_input, chatbot],
104
+ outputs=[state_history]
105
+ )
106
+
107
+ # ๋ฉ”์ธ ์‹คํ–‰
108
  if __name__ == "__main__":
109
+ demo.launch(server_name="0.0.0.0", server_port=7860, share=True)