Spaces:
Runtime error
Runtime error
| from google.colab import userdata | |
| import openai | |
| openai.api_key = userdata.get('OPENAI_API_KEY') | |
| # Initialize the OpenAI client | |
| from openai import OpenAI | |
| client = OpenAI(api_key=openai.api_key) | |
| from datetime import datetime | |
| # gpt function | |
| def chatgpt(prompt): | |
| today_day = datetime.now().strftime("%Y-%m-%d %H:%M:%S") # Get current date and time | |
| response = openai.chat.completions.create( | |
| model="gpt-4.5-preview-2025-02-27", # Use an actual available model | |
| 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}"}, | |
| {"role": "user", "content": prompt} | |
| ] | |
| ) | |
| return response.choices[0].message.content | |
| #Gradio | |
| import gradio as gr | |
| # Function to handle chat interaction | |
| def respond(message, chat_history): | |
| bot_message = chatgpt(message) # Assuming chatgpt is defined elsewhere | |
| chat_history.append((message, bot_message)) | |
| return "", chat_history | |
| # Create a Gradio Blocks interface | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("# Chat with GPT-4.5") | |
| gr.Markdown("Ask anything to gpt-4.5-preview-2025-02-27 model") | |
| chatbot = gr.Chatbot() | |
| with gr.Row(): | |
| msg = gr.Textbox(placeholder="Type your message here...", scale=4) | |
| send = gr.Button("Send", scale=1) | |
| clear = gr.Button("Clear") | |
| # Set up interactions | |
| send.click(respond, [msg, chatbot], [msg, chatbot]) | |
| msg.submit(respond, [msg, chatbot], [msg, chatbot]) # Keeps Enter key functionality | |
| clear.click(lambda: None, None, chatbot, queue=False) | |
| # Add example prompts | |
| gr.Examples( | |
| examples=["Tell me about quantum computing", | |
| "Write a short poem about AI", | |
| "How can I improve my Python skills?"], | |
| inputs=msg | |
| ) | |
| # Launch the app | |
| if __name__ == "__main__": | |
| demo.launch() | |