File size: 5,759 Bytes
7026716
0ec04e2
7026716
 
0ec04e2
f6fcb09
0ac657c
 
 
f6fcb09
8a48ab9
0ac657c
 
 
 
8a48ab9
 
 
e73ae5f
 
0ac657c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e73ae5f
 
0ec04e2
7026716
 
0ac657c
7026716
e73ae5f
7026716
 
 
0ec04e2
0ac657c
 
 
 
 
 
abca416
0ac657c
 
abca416
0ec04e2
7026716
 
 
 
 
0ec04e2
 
 
fe5d313
d192e97
452dd8e
0ac657c
abca416
8a48ab9
02aba1a
abca416
 
 
 
 
fe5d313
abca416
 
 
 
 
 
 
 
 
0ac657c
abca416
 
 
 
 
 
 
 
 
0ec04e2
0ac657c
1561b35
0ac657c
1561b35
0ac657c
 
1561b35
0ac657c
1561b35
83fea88
0ac657c
 
 
1561b35
 
 
 
 
 
 
 
d192e97
1561b35
fe5d313
 
 
9db7e9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ac657c
1561b35
0ac657c
338c269
0ac657c
 
1561b35
d192e97
0ac657c
fe5d313
0ac657c
 
 
fe5d313
0ec04e2
 
abca416
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
import os
import gradio as gr
from openai import OpenAI
from typing import List, Tuple

CLIENTS = {
    "perplexity": {"key": os.getenv('PX_KEY'), "endpoint": "https://api.perplexity.ai"},
    "hyperbolic": {"key": os.getenv('HYPERBOLIC_XYZ_KEY'), "endpoint": "https://api.hyperbolic.xyz/v1"},
    "huggingface": {"key": os.getenv('HF_KEY'), "endpoint": "https://huggingface.co/api/inference-proxy/together"},
}
for client_type in CLIENTS:
    CLIENTS[client_type]["client"] = OpenAI(
        base_url=CLIENTS[client_type]["endpoint"],
        api_key=CLIENTS[client_type]["key"]
    )

PASSWORD = os.getenv("PASSWD")

# Define available models
AVAILABLE_MODELS = {
    "Llama3.3-70b-Instruct (Hyperbolic.xyz)": {
        "model_name": "meta-llama/Llama-3.3-70B-Instruct",
        "type": "hyperbolic"
    },
    "Llama3.1-8b-Instruct (Hyperbolic.xyz)": {
        "model_name": "meta-llama/Meta-Llama-3.1-8B-Instruct",
        "type": "hyperbolic"
    },
    "DeepSeek V3 (Hyperbolic.xyz)": {
        "model_name": "deepseek-ai/DeepSeek-V3",
        "type": "hyperbolic"
    },
    "DeepSeek V3 (HuggingFace.co)": {
        "model_name": "deepseek-ai/DeepSeek-V3",
        "type": "huggingface"
    },
    "Sonar Pro (Perplexity.ai)": {
        "model_name": "sonar-pro",
        "type": "perplexity"
    },
    "Sonar (Perplexity.ai)": {
        "model_name": "sonar",
        "type": "perplexity"
    },
}

def respond(
    message: str,
    history: List[Tuple[str, str]],
    session_password: str,  # added parameter from session state
    system_message: str,
    model_choice: str,
    max_tokens: int,
    temperature: float,
    top_p: float,
):
    """Handles chatbot responses with password re-checking."""
    # Re-check the session password on every message
    if session_password != PASSWORD:
        yield "Error: Invalid session password. Please refresh the page and enter the correct password."
        return

    if model_choice not in AVAILABLE_MODELS:
        yield "Error: Invalid model selection."
        return

    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 = ""
    citations = []

    selected_client = CLIENTS[AVAILABLE_MODELS[model_choice]["type"]]["client"]

    try:
        stream = selected_client.chat.completions.create(
            model=AVAILABLE_MODELS[model_choice]["model_name"],
            messages=messages,
            max_tokens=max_tokens,
            temperature=temperature,
            top_p=top_p,
            stream=True,
        )

        for chunk in stream:
            if hasattr(chunk, "choices") and chunk.choices:
                token = chunk.choices[0].delta.content or ""
                response += token
                yield response  # Stream response as it arrives
            if hasattr(chunk, "citations") and chunk.citations:
                citations = chunk.citations

        # Append citations as clickable links, if any
        if citations:
            citation_text = "\n\nSources:\n" + "\n".join(
                [f"[{i+1}] [{url}]({url})" for i, url in enumerate(citations)]
            )
            response += citation_text
            yield response

    except Exception as e:
        yield f"Error: {str(e)}"


def check_password(input_password):
    """Validates the password and, if valid, stores it in session state."""
    if input_password == PASSWORD:
        # Return the password to store in the session state.
        return gr.update(visible=False), gr.update(visible=True), input_password
    else:
        return gr.update(value="", interactive=True), gr.update(visible=False), ""

with gr.Blocks() as demo:
    # A hidden state component to store the session password
    session_password = gr.State("")

    with gr.Column():
        password_input = gr.Textbox(
            type="password", label="Enter Password", interactive=True
        )
        submit_button = gr.Button("Submit")
        error_message = gr.Textbox(
            label="Error", visible=False, interactive=False
        )

    with gr.Column(visible=False) as chat_interface:
        system_prompt = gr.Textbox(
            value="You are a helpful assistant.", label="System message"
        )
        model_choice = gr.Dropdown(
            choices=list(AVAILABLE_MODELS.keys()),
            value=list(AVAILABLE_MODELS.keys())[0],
            label="Select Model"
        )
        max_tokens = gr.Slider(
            minimum=1, maximum=30000, value=2048, step=100, label="Max new tokens"
        )
        temperature = gr.Slider(
            minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"
        )
        top_p = gr.Slider(
            minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"
        )

        # Note: The session_password is now passed as an additional input to the chat function.
        chat = gr.ChatInterface(
            fn=respond,
            api_name=False,
            chatbot=gr.Chatbot(height=400),  # Set desired height here
            additional_inputs=[session_password, system_prompt, model_choice, max_tokens, temperature, top_p]
        )

    # Now, the submit_button click updates three outputs: the password_input, chat_interface visibility, and session_password state.
    submit_button.click(
        fn=check_password,
        inputs=password_input,
        outputs=[password_input, chat_interface, session_password]
    )

if __name__ == "__main__":
    demo.launch()