GreenBeanzz7 commited on
Commit
a7ee954
·
verified ·
1 Parent(s): c8f78d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -38
app.py CHANGED
@@ -1,52 +1,41 @@
1
  import os
2
  import gradio as gr
3
- import openai
4
-
5
- # Get API key from Hugging Face secrets
6
- api_key = os.getenv("OPENAI_API_KEY")
7
 
 
 
8
  if not api_key:
9
  raise ValueError("OPENAI_API_KEY not found. Please add it in Hugging Face Space secrets.")
10
 
11
- # Set API key globally for OpenAI
12
- openai.api_key = api_key
13
 
14
  # Chat function
15
- def chat_with_ai(message, history):
16
- messages = [{"role": "system", "content": "You are Liora, a helpful and supportive AI."}]
17
-
18
- for user, assistant in history:
19
- messages.append({"role": "user", "content": user})
20
- if assistant:
21
- messages.append({"role": "assistant", "content": assistant})
22
-
23
- messages.append({"role": "user", "content": message})
24
-
25
- try:
26
- response = openai.chat.completions.create(
27
- model="gpt-4o-mini",
28
- messages=messages
29
- )
30
- reply = response.choices[0].message.content
31
- return reply
32
- except Exception as e:
33
- return f"Error: {str(e)}"
34
-
35
- # Gradio Chatbot
36
  with gr.Blocks() as demo:
37
- gr.Markdown("## 🌌 SilentCurrent Powered by Liora")
38
- chatbot = gr.Chatbot()
39
- msg = gr.Textbox(placeholder="Type your message here...")
40
- clear = gr.Button("Clear")
 
 
 
41
 
42
- def user_message(user_message, history):
43
- reply = chat_with_ai(user_message, history)
44
- history.append((user_message, reply))
45
- return history, ""
46
 
47
- msg.submit(user_message, [msg, chatbot], [chatbot, msg])
48
- clear.click(lambda: None, None, chatbot, queue=False)
49
 
50
- # Launch
51
  if __name__ == "__main__":
52
  demo.launch()
 
1
  import os
2
  import gradio as gr
3
+ from openai import OpenAI
 
 
 
4
 
5
+ # Load API key from Hugging Face Space secrets
6
+ api_key = os.environ.get("OPENAI_API_KEY")
7
  if not api_key:
8
  raise ValueError("OPENAI_API_KEY not found. Please add it in Hugging Face Space secrets.")
9
 
10
+ # Initialize OpenAI client
11
+ client = OpenAI(api_key=api_key)
12
 
13
  # Chat function
14
+ def chat_with_openai(messages, user_input):
15
+ messages.append({"role": "user", "content": user_input})
16
+ response = client.chat.completions.create(
17
+ model="gpt-4o-mini",
18
+ messages=messages
19
+ )
20
+ reply = response.choices[0].message.content
21
+ messages.append({"role": "assistant", "content": reply})
22
+ return messages, messages
23
+
24
+ # Gradio interface
 
 
 
 
 
 
 
 
 
 
25
  with gr.Blocks() as demo:
26
+ gr.Markdown("## 🌌 SilentCurrent Powered by OpenAI")
27
+
28
+ chatbot = gr.Chatbot(type="messages")
29
+ state = gr.State([])
30
+
31
+ with gr.Row():
32
+ txt = gr.Textbox(show_label=False, placeholder="Type your message here...")
33
 
34
+ def respond(user_input, state):
35
+ return chat_with_openai(state, user_input)
 
 
36
 
37
+ txt.submit(respond, [txt, state], [chatbot, state])
 
38
 
39
+ # Launch app
40
  if __name__ == "__main__":
41
  demo.launch()