File size: 6,436 Bytes
5305e57
599d7c0
a47572b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5305e57
 
 
a47572b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5305e57
599d7c0
 
a47572b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
599d7c0
a47572b
 
 
 
599d7c0
a47572b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
599d7c0
a47572b
599d7c0
a47572b
599d7c0
 
a47572b
599d7c0
a47572b
599d7c0
 
a47572b
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import os
from openai import OpenAI
from datetime import datetime
import gradio as gr
import time

# --- Constants ---
DEFAULT_MODEL = "gpt-4.5-preview-2025-02-27"  # Assuming gpt-4o is a good default
DEFAULT_TEMPERATURE = 1.0 # Match your example
DEFAULT_TOP_P = 1.0       # Match your example
DEFAULT_FREQ_PENALTY = 0  # Match your example
DEFAULT_PRES_PENALTY = 0  # Match your example
MAX_TOKENS = 2048       # Match your example
MAX_HISTORY_LENGTH = 5

# --- API Key and Client Initialization ---
import openai
API_KEY = os.getenv("OPENAI_API_KEY")
client = OpenAI(api_key=API_KEY)

# --- Helper Functions ---
def get_openai_response(prompt, model=DEFAULT_MODEL, temperature=DEFAULT_TEMPERATURE, top_p=DEFAULT_TOP_P,
                        frequency_penalty=DEFAULT_FREQ_PENALTY, presence_penalty=DEFAULT_PRES_PENALTY,
                        max_tokens=MAX_TOKENS, system_prompt="", chat_history=None):
    """Gets a response from the OpenAI API, handling errors and streaming."""
    today_day = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    try:
        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}]
        if chat_history:
            for turn in chat_history:
                messages.append({"role": "user", "content": turn[0]})
                messages.append({"role": "assistant", "content": turn[1]})
        messages.append({"role": "user", "content": prompt})

        response = client.chat.completions.create(
            model=model,
            messages=messages,
            temperature=temperature,
            max_tokens=max_tokens, #Use the new name
            top_p=top_p,
            frequency_penalty=frequency_penalty,
            presence_penalty=presence_penalty,
            response_format={"type": "text"},  # As per your example
            stream=True  # Enable streaming!
        )

        collected_messages = []
        for chunk in response:
            chunk_message = chunk.choices[0].delta.content
            if chunk_message is not None:
                collected_messages.append(chunk_message)
            full_reply_content = ''.join(collected_messages)
            yield full_reply_content

    except openai.APIConnectionError as e:
        return f"Error: Could not connect to OpenAI API: {e}"
    except openai.RateLimitError as e:
        return f"Error: Rate limit exceeded: {e}"
    except openai.APIStatusError as e:
        return f"Error: OpenAI API returned an error: {e}"
    except Exception as e:
        return f"An unexpected error occurred: {e}"



def update_ui(message, chat_history, model, temperature, top_p, frequency_penalty, presence_penalty, system_prompt, history_length):
    """Updates the Gradio UI; handles streaming response."""
    bot_message_gen = get_openai_response(
        prompt=message, model=model, temperature=temperature, top_p=top_p,
        frequency_penalty=frequency_penalty, presence_penalty=presence_penalty,
        system_prompt=system_prompt, chat_history=chat_history
    )
    chat_history.append((message, ""))
    for bot_message in bot_message_gen:
        chat_history[-1] = (chat_history[-1][0], bot_message)
        visible_history = chat_history[-history_length:]
        time.sleep(0.025) #Rate limiter
        yield "", visible_history

# --- Gradio Interface ---
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("# Chat with GPT (Gradio)")
    gr.Markdown("Made by: [@diegocabezas01](https://x.com/diegocabezas01) on X")
    gr.Markdown("☕ [Support my work](https://buymeacoffee.com/diegocp01m)")

    with gr.Row():
        with gr.Column(scale=4):
            chatbot = gr.Chatbot(
                show_label=False,
                avatar_images=(
                    "https://cdn-icons-png.flaticon.com/512/8428/8428718.png",  # User image URL
                    "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ef/ChatGPT-Logo.svg/640px-ChatGPT-Logo.svg.png"  # OpenAI image URL
                ),
                render_markdown=True,
                height=500
            )
            msg = gr.Textbox(placeholder="Type your message here...", scale=4, show_label=False)

            with gr.Accordion("Advanced Options", open=False):
                model_select = gr.Dropdown(
                    label="Model",
                    choices=["gpt-4.5-preview-2025-02-27", "gpt-4o-mini-2024-07-18"],  # Update with your models
                    value=DEFAULT_MODEL,
                    interactive=True
                )
                temperature_slider = gr.Slider(label="Temperature", minimum=0.0, maximum=2.0, value=DEFAULT_TEMPERATURE, step=0.1, interactive=True)
                top_p_slider = gr.Slider(label="Top P", minimum=0.0, maximum=1.0, value=DEFAULT_TOP_P, step=0.05, interactive=True)
                frequency_penalty_slider = gr.Slider(label="Frequency Penalty", minimum=-2.0, maximum=2.0, value=DEFAULT_FREQ_PENALTY, step=0.1, interactive=True)
                presence_penalty_slider = gr.Slider(label="Presence Penalty", minimum=-2.0, maximum=2.0, value=DEFAULT_PRES_PENALTY, step=0.1, interactive=True)
                system_prompt_textbox = gr.Textbox(label="System Prompt", placeholder="Enter a custom system prompt...", lines=3, interactive=True)
                history_length_slider = gr.Slider(label="Chat History Length", minimum=1, maximum=20, value=MAX_HISTORY_LENGTH, step=1, interactive=True)


            with gr.Row():
                send = gr.Button("Send")
                clear = gr.Button("Clear")

    # --- Event Handlers ---
    send_event = send.click(
        update_ui,
        [msg, chatbot, model_select, temperature_slider, top_p_slider, frequency_penalty_slider, presence_penalty_slider, system_prompt_textbox, history_length_slider],
        [msg, chatbot]
    )
    msg.submit(
        update_ui,
        [msg, chatbot, model_select, temperature_slider, top_p_slider, frequency_penalty_slider, presence_penalty_slider, system_prompt_textbox, history_length_slider],
        [msg, chatbot]
    )
    clear.click(lambda: None, None, chatbot, queue=False)

    gr.Examples(
        examples=["Tell me about quantum computing", "Write a short poem about AI", "How can I improve my Python skills?"],
        inputs=msg
    )
    msg.focus()

# --- Launch ---
if __name__ == "__main__":
    demo.launch()