File size: 2,407 Bytes
0ebb816
8c851ec
0ebb816
 
 
 
 
 
 
 
 
 
 
 
 
e84f436
 
 
 
 
 
 
0ebb816
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e20f4b
 
 
 
 
 
 
9d1039f
184e7e1
0ebb816
 
 
 
 
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
import os
import logging

import gradio as gr
import anthropic
from dotenv import load_dotenv

load_dotenv(".env")

logging.basicConfig(level=logging.INFO)
logging.getLogger("gradio").setLevel(logging.INFO)
logging.getLogger("httpx").setLevel(logging.WARNING)


def generate_completion(
    input,
    history,
    api_key,
    model,
    system_prompt,
    temperature,
    max_tokens,
):
    if os.getenv("ANTHROPIC_API_KEY"):
        api_key = os.getenv("ANTHROPIC_API_KEY")

    if not api_key:
        # raise ValueError("API Key is required")
        yield "No API key provided"

    client = anthropic.Anthropic(
        api_key=api_key,
    )

    messages = []

    if history:
        for entry in history:
            if len(entry) == 2:
                messages.append(
                    {
                        "role": "user",
                        "content": entry[0],
                    }
                )
                messages.append(
                    {
                        "role": "assistant",
                        "content": entry[1],
                    }
                )

    messages.append(
        {
            "role": "user",
            "content": input,
        }
    )

    with client.messages.stream(
        model=model,
        max_tokens=max_tokens,
        temperature=temperature,
        system=system_prompt,
        messages=messages,
    ) as stream:
        answer_str = ""
        for text in stream.text_stream:
            # print(text, end="", flush=True)
            answer_str += text
            yield answer_str


api_key = gr.Textbox(label="API Key", type="password")
model = gr.Textbox(label="Model", value="claude-3-opus-20240229")
system_prompt = gr.Textbox(
    label="System Prompt",
    value="You are a world-class assistant.",
)
temperature = gr.Slider(label="Temperature", value=0.0, minimum=0.0, maximum=1.0)
max_tokens = gr.Slider(label="Max Tokens", value=4096, minimum=1, maximum=4096)


demo = gr.ChatInterface(
    fn=generate_completion,
    additional_inputs=[
        api_key,
        model,
        system_prompt,
        temperature,
        max_tokens,
    ],
    description="Claude Chatbot, Clone the space and add your own API key in the 'additional inputs' section. Get your key here: https://console.anthropic.com/login",
    fill_height=True,
)

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