Tabatha-625 commited on
Commit
c82b138
·
verified ·
1 Parent(s): 7b97d2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -10
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from openai import OpenAI
3
  import os
4
 
5
  client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
@@ -8,25 +8,49 @@ def chat(message):
8
  response = client.chat.completions.create(
9
  model="gpt-4o-mini",
10
  messages=[
11
- {"role": "system", "content": "You are a helpful AI assistant."},
12
  {"role": "user", "content": message}
13
  ]
14
  )
15
  return response.choices[0].message.content
16
 
17
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  gr.Markdown(
19
  """
20
- # 🤖 AI Chat (Powered by ChatGPT)
21
- This chatbot uses OpenAI’s ChatGPT.
 
 
22
  """
23
  )
24
 
25
- user_input = gr.Textbox(label="Your message")
26
- output = gr.Textbox(label="ChatGPT response", lines=10)
27
- send = gr.Button("Send")
 
 
 
 
 
 
 
 
28
 
29
- send.click(chat, inputs=user_input, outputs=output)
 
30
 
31
  demo.launch()
32
-
 
1
  import gradio as gr
2
+ from openai import OpenAI
3
  import os
4
 
5
  client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
 
8
  response = client.chat.completions.create(
9
  model="gpt-4o-mini",
10
  messages=[
11
+ {"role": "system", "content": "You are a helpful assistant."},
12
  {"role": "user", "content": message}
13
  ]
14
  )
15
  return response.choices[0].message.content
16
 
17
+ css = """
18
+ body {
19
+ background-color: #0f0f0f;
20
+ }
21
+ .gradio-container {
22
+ max-width: 700px !important;
23
+ margin: auto;
24
+ }
25
+ textarea {
26
+ background-color: #1e1e1e !important;
27
+ color: white !important;
28
+ }
29
+ """
30
+
31
+ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
32
  gr.Markdown(
33
  """
34
+ <h1 style="text-align:center;">💬 ChatGPT‑style AI</h1>
35
+ <p style="text-align:center; color:gray;">
36
+ Powered by OpenAI
37
+ </p>
38
  """
39
  )
40
 
41
+ chatbot = gr.Chatbot(height=400)
42
+ msg = gr.Textbox(
43
+ placeholder="Send a message...",
44
+ show_label=False
45
+ )
46
+ clear = gr.Button("Clear")
47
+
48
+ def respond(message, history):
49
+ reply = chat(message)
50
+ history.append((message, reply))
51
+ return "", history
52
 
53
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
54
+ clear.click(lambda: [], None, chatbot)
55
 
56
  demo.launch()