diegocp01 commited on
Commit
a47572b
·
verified ·
1 Parent(s): 74caf6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -42
app.py CHANGED
@@ -1,59 +1,142 @@
1
-
2
- import openai
3
  import os
4
-
5
- # Initialize the OpenAI client
6
  from openai import OpenAI
7
- # HF
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  API_KEY = os.getenv("OPENAI_API_KEY")
9
  client = OpenAI(api_key=API_KEY)
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- from datetime import datetime
13
- # gpt function
14
- def chatgpt(prompt):
15
- today_day = datetime.now().strftime("%Y-%m-%d %H:%M:%S") # Get current date and time
16
- response = openai.chat.completions.create(
17
- model="gpt-4.5-preview-2025-02-27", # Use an actual available model
18
- messages=[
19
- {"role": "system", "content": f"You are GPT 4.5 the latest model from OpenAI, released Feb, 27th 2025 at 3 pm ET. Todays date is: {today_day}"},
20
- {"role": "user", "content": prompt}
21
- ]
22
- )
23
- return response.choices[0].message.content
24
- #Gradio
25
- import gradio as gr
26
 
27
- # Function to handle chat interaction
28
- def respond(message, chat_history):
29
- bot_message = chatgpt(message) # Assuming chatgpt is defined elsewhere
30
- chat_history.append((message, bot_message))
31
- return "", chat_history
32
 
33
- # Create a Gradio Blocks interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
35
- gr.Markdown("# Chat with GPT-4.5")
36
- gr.Markdown("Ask anything to gpt-4.5-preview-2025-02-27 model. Made by: @diegocabezas01 [@diegocabezas01](https://x.com/diegocabezas01) on X (prev. Twitter)")
37
-
38
- chatbot = gr.Chatbot()
39
  with gr.Row():
40
- msg = gr.Textbox(placeholder="Type your message here...", scale=4)
41
- send = gr.Button("Send", scale=1)
42
- clear = gr.Button("Clear")
43
-
44
- # Set up interactions
45
- send.click(respond, [msg, chatbot], [msg, chatbot])
46
- msg.submit(respond, [msg, chatbot], [msg, chatbot]) # Keeps Enter key functionality
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  clear.click(lambda: None, None, chatbot, queue=False)
48
-
49
- # Add example prompts
50
  gr.Examples(
51
- examples=["Tell me about quantum computing",
52
- "Write a short poem about AI",
53
- "How can I improve my Python skills?"],
54
  inputs=msg
55
  )
 
56
 
57
- # Launch the app
58
  if __name__ == "__main__":
59
  demo.launch()
 
 
 
 
 
 
1
  import os
 
 
2
  from openai import OpenAI
3
+ from datetime import datetime
4
+ import gradio as gr
5
+ import time
6
+
7
+ # --- Constants ---
8
+ DEFAULT_MODEL = "gpt-4.5-preview-2025-02-27" # Assuming gpt-4o is a good default
9
+ DEFAULT_TEMPERATURE = 1.0 # Match your example
10
+ DEFAULT_TOP_P = 1.0 # Match your example
11
+ DEFAULT_FREQ_PENALTY = 0 # Match your example
12
+ DEFAULT_PRES_PENALTY = 0 # Match your example
13
+ MAX_TOKENS = 2048 # Match your example
14
+ MAX_HISTORY_LENGTH = 5
15
+
16
+ # --- API Key and Client Initialization ---
17
+ import openai
18
  API_KEY = os.getenv("OPENAI_API_KEY")
19
  client = OpenAI(api_key=API_KEY)
20
 
21
+ # --- Helper Functions ---
22
+ def get_openai_response(prompt, model=DEFAULT_MODEL, temperature=DEFAULT_TEMPERATURE, top_p=DEFAULT_TOP_P,
23
+ frequency_penalty=DEFAULT_FREQ_PENALTY, presence_penalty=DEFAULT_PRES_PENALTY,
24
+ max_tokens=MAX_TOKENS, system_prompt="", chat_history=None):
25
+ """Gets a response from the OpenAI API, handling errors and streaming."""
26
+ today_day = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
27
+ try:
28
+ messages = [{"role": "system", "content": f"You are GPT 4.5 the latest model from OpenAI, released Feb, 27th 2025 at 3 pm ET. Todays date is: {today_day} " + system_prompt}]
29
+ if chat_history:
30
+ for turn in chat_history:
31
+ messages.append({"role": "user", "content": turn[0]})
32
+ messages.append({"role": "assistant", "content": turn[1]})
33
+ messages.append({"role": "user", "content": prompt})
34
+
35
+ response = client.chat.completions.create(
36
+ model=model,
37
+ messages=messages,
38
+ temperature=temperature,
39
+ max_tokens=max_tokens, #Use the new name
40
+ top_p=top_p,
41
+ frequency_penalty=frequency_penalty,
42
+ presence_penalty=presence_penalty,
43
+ response_format={"type": "text"}, # As per your example
44
+ stream=True # Enable streaming!
45
+ )
46
+
47
+ collected_messages = []
48
+ for chunk in response:
49
+ chunk_message = chunk.choices[0].delta.content
50
+ if chunk_message is not None:
51
+ collected_messages.append(chunk_message)
52
+ full_reply_content = ''.join(collected_messages)
53
+ yield full_reply_content
54
+
55
+ except openai.APIConnectionError as e:
56
+ return f"Error: Could not connect to OpenAI API: {e}"
57
+ except openai.RateLimitError as e:
58
+ return f"Error: Rate limit exceeded: {e}"
59
+ except openai.APIStatusError as e:
60
+ return f"Error: OpenAI API returned an error: {e}"
61
+ except Exception as e:
62
+ return f"An unexpected error occurred: {e}"
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
 
 
 
 
 
65
 
66
+ def update_ui(message, chat_history, model, temperature, top_p, frequency_penalty, presence_penalty, system_prompt, history_length):
67
+ """Updates the Gradio UI; handles streaming response."""
68
+ bot_message_gen = get_openai_response(
69
+ prompt=message, model=model, temperature=temperature, top_p=top_p,
70
+ frequency_penalty=frequency_penalty, presence_penalty=presence_penalty,
71
+ system_prompt=system_prompt, chat_history=chat_history
72
+ )
73
+ chat_history.append((message, ""))
74
+ for bot_message in bot_message_gen:
75
+ chat_history[-1] = (chat_history[-1][0], bot_message)
76
+ visible_history = chat_history[-history_length:]
77
+ time.sleep(0.025) #Rate limiter
78
+ yield "", visible_history
79
+
80
+ # --- Gradio Interface ---
81
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
82
+ gr.Markdown("# Chat with GPT (Gradio)")
83
+ gr.Markdown("Made by: [@diegocabezas01](https://x.com/diegocabezas01) on X")
84
+ gr.Markdown("☕ [Support my work](https://buymeacoffee.com/diegocp01m)")
85
+
86
  with gr.Row():
87
+ with gr.Column(scale=4):
88
+ chatbot = gr.Chatbot(
89
+ show_label=False,
90
+ avatar_images=(
91
+ "https://cdn-icons-png.flaticon.com/512/8428/8428718.png", # User image URL
92
+ "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ef/ChatGPT-Logo.svg/640px-ChatGPT-Logo.svg.png" # OpenAI image URL
93
+ ),
94
+ render_markdown=True,
95
+ height=500
96
+ )
97
+ msg = gr.Textbox(placeholder="Type your message here...", scale=4, show_label=False)
98
+
99
+ with gr.Accordion("Advanced Options", open=False):
100
+ model_select = gr.Dropdown(
101
+ label="Model",
102
+ choices=["gpt-4.5-preview-2025-02-27", "gpt-4o-mini-2024-07-18"], # Update with your models
103
+ value=DEFAULT_MODEL,
104
+ interactive=True
105
+ )
106
+ temperature_slider = gr.Slider(label="Temperature", minimum=0.0, maximum=2.0, value=DEFAULT_TEMPERATURE, step=0.1, interactive=True)
107
+ top_p_slider = gr.Slider(label="Top P", minimum=0.0, maximum=1.0, value=DEFAULT_TOP_P, step=0.05, interactive=True)
108
+ frequency_penalty_slider = gr.Slider(label="Frequency Penalty", minimum=-2.0, maximum=2.0, value=DEFAULT_FREQ_PENALTY, step=0.1, interactive=True)
109
+ presence_penalty_slider = gr.Slider(label="Presence Penalty", minimum=-2.0, maximum=2.0, value=DEFAULT_PRES_PENALTY, step=0.1, interactive=True)
110
+ system_prompt_textbox = gr.Textbox(label="System Prompt", placeholder="Enter a custom system prompt...", lines=3, interactive=True)
111
+ history_length_slider = gr.Slider(label="Chat History Length", minimum=1, maximum=20, value=MAX_HISTORY_LENGTH, step=1, interactive=True)
112
+
113
+
114
+ with gr.Row():
115
+ send = gr.Button("Send")
116
+ clear = gr.Button("Clear")
117
+
118
+ # --- Event Handlers ---
119
+ send_event = send.click(
120
+ update_ui,
121
+ [msg, chatbot, model_select, temperature_slider, top_p_slider, frequency_penalty_slider, presence_penalty_slider, system_prompt_textbox, history_length_slider],
122
+ [msg, chatbot]
123
+ )
124
+ msg.submit(
125
+ update_ui,
126
+ [msg, chatbot, model_select, temperature_slider, top_p_slider, frequency_penalty_slider, presence_penalty_slider, system_prompt_textbox, history_length_slider],
127
+ [msg, chatbot]
128
+ )
129
  clear.click(lambda: None, None, chatbot, queue=False)
130
+
 
131
  gr.Examples(
132
+ examples=["Tell me about quantum computing", "Write a short poem about AI", "How can I improve my Python skills?"],
 
 
133
  inputs=msg
134
  )
135
+ msg.focus()
136
 
137
+ # --- Launch ---
138
  if __name__ == "__main__":
139
  demo.launch()
140
+
141
+
142
+