File size: 7,454 Bytes
44d1f22
 
73f5b3d
44d1f22
 
 
 
 
 
 
a294b9c
73f5b3d
 
a294b9c
44d1f22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a294b9c
 
44d1f22
a294b9c
44d1f22
 
 
 
 
 
 
 
 
a294b9c
 
44d1f22
 
 
 
 
a294b9c
 
 
 
44d1f22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a294b9c
73f5b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44d1f22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73f5b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44d1f22
73f5b3d
 
 
 
 
a294b9c
73f5b3d
 
 
44d1f22
73f5b3d
 
 
 
 
44d1f22
73f5b3d
 
 
44d1f22
 
a294b9c
73f5b3d
44d1f22
 
a294b9c
 
73f5b3d
 
 
 
 
 
 
 
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
from __future__ import annotations

import os
from typing import Generator

import gradio as gr
from litellm import completion
from litellm import model_list
from litellm.utils import get_valid_models


# Create static directory if it doesn't exist
os.makedirs("static", exist_ok=True)


def get_available_models(
    provider: str,
    api_key: str | None = None,
) -> list[str]:
    """Get available models from LiteLLM for the specified provider"""
    try:
        if api_key:
            os.environ[f"{provider.upper()}_API_KEY"] = api_key
            try:
                # Try to get models from API
                models = model_list(provider)
                return [model["id"] for model in models]
            except Exception:
                # Fallback to LiteLLM's valid models for the provider
                valid_models = get_valid_models()
                provider_models = [
                    model.split("/")[-1] if "/" in model else model
                    for model in valid_models
                    if model.startswith(f"{provider}/") or model.startswith(provider)
                ]
                return provider_models if provider_models else ["gpt-3.5-turbo"]
        return ["gpt-3.5-turbo"]  # Default fallback
    except Exception as e:
        print(f"Error fetching models: {e!s}")
        return ["gpt-3.5-turbo"]  # Fallback on error


def respond(
    message: str,
    history: list[tuple[str, str]],
    system_message: str,
    max_tokens: int,
    temperature: float,
    top_p: float,
    provider: str,
    model: str,
    api_key: str,
) -> Generator[str, None, None]:
    """Generate chat responses using the specified model and provider"""
    messages = [{"role": "system", "content": system_message}]

    for user_msg, assistant_msg in history:
        if user_msg:
            messages.append({"role": "user", "content": user_msg})
        if assistant_msg:
            messages.append({"role": "assistant", "content": assistant_msg})

    messages.append({"role": "user", "content": message})
    response = ""

    # Set API key if provided
    if api_key:
        os.environ[f"{provider.upper()}_API_KEY"] = api_key

    try:
        # Construct full model name if needed
        model_name = model if "/" in model else f"{provider}/{model}"

        for chunk in completion(
            model=model_name,
            messages=messages,
            max_tokens=max_tokens,
            temperature=temperature,
            top_p=top_p,
            stream=True,
        ):
            token = chunk.choices[0].delta.content
            if token:
                response += token
                yield response
    except Exception as e:
        yield f"Error: {e!s}"


def update_model_list(provider: str, api_key: str) -> gr.Dropdown:
    """Update the model dropdown based on provider and API key"""
    models = get_available_models(provider, api_key)
    return gr.Dropdown(choices=models, value=models[0] if models else None)


def clear_click() -> None:
    """Clear the chat history"""
    return None


def clear_input() -> str:
    """Clear the input textbox"""
    return ""


# Get available providers from LiteLLM
valid_models = get_valid_models()
providers = sorted({model.split("/")[0] for model in valid_models if "/" in model})

# Create the chat interface with enhanced styling
with gr.Blocks(
    css="static/styles.css",
    title="AI Chat Assistant",
    theme=gr.themes.Soft(
        primary_hue="blue",
        secondary_hue="blue",
        neutral_hue="slate",
        radius_size=gr.themes.sizes.radius_sm,
    ),
) as demo:
    with gr.Column(elem_classes="chat-container"):
        chatbot = gr.Chatbot(
            label="Chat History",
            bubble_full_width=False,
            show_label=False,
            elem_classes=["chat-history"],
            height=500,
        )
        msg = gr.Textbox(
            label="Type your message",
            placeholder="Enter your message here...",
            show_label=False,
            container=False,
            scale=8,
        )
        with gr.Row():
            submit = gr.Button("Send", variant="primary", scale=1)
            clear = gr.Button("Clear", variant="secondary", scale=1)

    with gr.Accordion("Model Settings", open=True, elem_classes="additional-inputs"):
        with gr.Row():
            provider = gr.Dropdown(
                choices=providers,
                value=providers[0] if providers else "openai",
                label="Provider",
                info="Select the AI provider",
            )
            api_key = gr.Textbox(
                value="",
                label="API Key",
                info="Enter your API key",
                type="password",
            )
            model = gr.Dropdown(
                choices=get_available_models(providers[0] if providers else "openai"),
                value="gpt-3.5-turbo",
                label="Model",
                info="Select the model to use",
            )

        # Update model list when provider or API key changes
        provider.change(
            update_model_list,
            inputs=[provider, api_key],
            outputs=model,
        )
        api_key.change(
            update_model_list,
            inputs=[provider, api_key],
            outputs=model,
        )

    with gr.Accordion("Chat Settings", open=False, elem_classes="additional-inputs"):
        system_message = gr.Textbox(
            value="You are a friendly and helpful AI assistant.",
            label="System Message",
            info="Set the AI's personality and behavior",
        )
        with gr.Row():
            with gr.Column():
                temperature = gr.Slider(
                    minimum=0.1,
                    maximum=2.0,
                    value=0.7,
                    step=0.1,
                    label="Temperature",
                    info="Higher values make responses more creative but less focused",
                )
                top_p = gr.Slider(
                    minimum=0.1,
                    maximum=1.0,
                    value=0.95,
                    step=0.05,
                    label="Top-p (nucleus sampling)",
                    info="Controls response diversity",
                )
            with gr.Column():
                max_tokens = gr.Slider(
                    minimum=1,
                    maximum=327670,
                    value=512,
                    step=1,
                    label="Max Tokens",
                    info="Maximum length of the response",
                )

    # Set up chat functionality
    msg_submit_trigger = msg.submit(
        respond,
        [msg, chatbot, system_message, max_tokens, temperature, top_p, provider, model, api_key],
        [chatbot],
        api_name="chat",
    )
    submit_click_trigger = submit.click(
        respond,
        [msg, chatbot, system_message, max_tokens, temperature, top_p, provider, model, api_key],
        [chatbot],
        api_name="chat",
    )

    clear.click(clear_click, None, chatbot, queue=False)

    # Clear input after sending
    msg_submit_trigger.then(clear_input, None, msg)
    submit_click_trigger.then(clear_input, None, msg)

if __name__ == "__main__":
    demo.launch(
        share=True,
        server_name="0.0.0.0",
        server_port=7860,
        show_api=False,
        favicon_path="🤖",
        allowed_paths=["static"],
    )