File size: 5,215 Bytes
42cdc8f
 
35cce96
 
42cdc8f
35cce96
42cdc8f
 
35cce96
 
 
 
42cdc8f
 
 
 
35cce96
42cdc8f
35cce96
42cdc8f
 
 
35cce96
 
 
42cdc8f
 
 
 
 
 
 
35cce96
42cdc8f
 
 
 
35cce96
 
 
 
 
 
 
42cdc8f
 
 
 
 
 
 
 
 
 
 
35cce96
 
 
 
 
 
 
 
 
42cdc8f
 
 
 
 
 
 
35cce96
42cdc8f
 
 
 
35cce96
 
 
 
 
 
 
42cdc8f
 
 
 
 
 
 
 
 
 
 
 
 
35cce96
 
42cdc8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35cce96
42cdc8f
 
 
 
 
 
 
 
 
 
 
 
 
35cce96
42cdc8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35cce96
 
 
42cdc8f
 
 
 
 
 
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 re

import anthropic
import gradio as gr
from gradio.components import Checkbox, Dropdown, IOComponent, Textbox

from ai import AnthropicCustom
from const import ClaudeModels, ModelTokenLength, Prompts

# Define a global variable for the conversation history
conversation_history = ""


async def interact_with_ai(
    user_question, token, model, token_length, prompt, prompt_input, memory
):
    global conversation_history

    if memory:
        prompt = Prompts[prompt].value.format(
            memory=conversation_history, question=user_question
        )
    else:
        prompt = Prompts[prompt].value.format(memory="", question=user_question)

    if prompt_input != re.search(r"Human: (.*?) \n\nConversations:", prompt).group(1):
        prompt = re.sub(
            r"Human: (.*?) \n\nConversations:",
            f"Human: {prompt_input} \n\nConversations:",
            prompt,
        )

    # Create an instance of the custom class
    anth = AnthropicCustom(
        api_key=token, model=model, max_tokens=token_length, prompt=prompt
    )

    # Create a generator to stream the response
    response_accumulated = ""
    async for response in anth.get_anthropic_response_async():
        response_accumulated += response
        conversation_history = f"{conversation_history} {anthropic.HUMAN_PROMPT} {user_question} {anthropic.AI_PROMPT} {response_accumulated}"
        yield response_accumulated


async def chat_with_ai(
    message,
    history,
    token,
    model,
    token_length,
    prompt,
    prompt_input,
    memory,
):
    global conversation_history
    if memory:
        for conversation in history:
            user_question, response_accumulated = conversation
            conversation_history = f"{conversation_history} {anthropic.HUMAN_PROMPT} {user_question} {anthropic.AI_PROMPT} {response_accumulated}"
        prompt = Prompts[prompt].value.format(memory=history, question=message)
    else:
        prompt = Prompts[prompt].value.format(memory="", question=message)

    if prompt_input != re.search(r"Human: (.*?) \n\nConversations:", prompt).group(1):
        prompt = re.sub(
            r"Human: (.*?) \n\nConversations:",
            f"Human: {prompt_input} \n\nConversations:",
            prompt,
        )

    # Create an instance of the custom class
    anth = AnthropicCustom(
        api_key=token, model=model, max_tokens=token_length, prompt=prompt
    )

    # Create a generator to stream the response
    response_accumulated = ""
    async for response in anth.get_anthropic_response_async():
        response_accumulated += response
        yield response_accumulated


promptDropdown: IOComponent = Dropdown(
    choices=list(Prompts.__members__.keys()),
    label="Prompt",
    value=list(Prompts.__members__.keys())[0],
)
prompt_input: IOComponent = Textbox(
    label="Custom Prompt",
    placeholder="Enter a custom prompt here",
    lines=3,
    value=re.search(
        r"Human: (.*?) \n\nConversations:", Prompts[promptDropdown.value].value
    ).group(1),
)


iface = gr.Interface(
    fn=interact_with_ai,
    flagging_options=["Inappropriate", "Disrespectful", "Spam"],
    allow_flagging="auto",
    title="Claude Space",
    inputs=[
        Textbox(label="Question", placeholder="Enter a question here"),
        Textbox(label="Token", placeholder="Enter a token here", type="password"),
        Dropdown(
            choices=[model.value for model in ClaudeModels],
            label="Model",
            value=[model.value for model in ClaudeModels][0],
        ),
        Dropdown(
            choices=[token.value for token in ModelTokenLength],
            label="Token Length",
            value=[token.value for token in ModelTokenLength][0],
        ),
        promptDropdown,
        prompt_input,
        Checkbox(label="Memory", value=False),
    ],
    outputs="markdown",
    cache_examples=True,
)

promptDropdown: IOComponent = Dropdown(
    choices=list(Prompts.__members__.keys()),
    label="Prompt",
    value=list(Prompts.__members__.keys())[0],
)
prompt_input: IOComponent = Textbox(
    label="Custom Prompt",
    placeholder="Enter a custom prompt here",
    lines=3,
    value=re.search(
        r"Human: (.*?) \n\nConversations:", Prompts[promptDropdown.value].value
    ).group(1),
)

cface = gr.ChatInterface(
    fn=chat_with_ai,
    additional_inputs=[
        Textbox(label="Token", placeholder="Enter a token here", type="password"),
        Dropdown(
            choices=[model.value for model in ClaudeModels],
            label="Model",
            value=[model.value for model in ClaudeModels][0],
        ),
        Dropdown(
            choices=[token.value for token in ModelTokenLength],
            label="Token Length",
            value=[token.value for token in ModelTokenLength][0],
        ),
        promptDropdown,
        prompt_input,
        Checkbox(label="Memory", value=True),
    ],
)


if __name__ == "__main__":
    gd = gr.TabbedInterface(
        [iface, cface], tab_names=["Claude Space", "Claude Chat"], title="Claude Space"
    )
    gd.queue(concurrency_count=75, max_size=100).launch(
        debug=True, share=False, server_name="0.0.0.0", server_port=7864
    )