-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
53 lines (38 loc) · 1.36 KB
/
main.py
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
import os
import gradio as gr
import anthropic
client = anthropic.Anthropic(api_key=os.environ['ANTHROPIC_API_KEY'])
MODEL = "claude-3-5-sonnet-20240620"
def chat_with_replit(message, history):
messages = []
response_content = ""
for h in history:
u = str(h[0])
s = str(h[1])
messages.append({"role": 'user', "content": u})
messages.append({"role": 'assistant', "content": s})
messages = messages + [
{
"role": "user",
"content": str(message),
},
]
with client.messages.stream(
max_tokens=1024,
messages=messages,
model=MODEL,
) as stream:
for text in stream.text_stream:
response_content += text
yield response_content
js = """<script src="https://replit.com/public/js/replit-badge-v2.js" theme="dark" position="bottom-right"></script>"""
with gr.Blocks(title="Replit + Anthropic", fill_height=True, head=js) as demo:
gr.ChatInterface(chat_with_replit,
fill_height=True,
examples=[
"What is the meaning of life?",
"What are some fun things to do in San Francisco?",
"Interpret 'The Road Not Taken' by Robert Frost"
],
title="🚀 Chat with Claude Sonnet 3.5")
demo.launch(favicon_path="assets/favicon.png", allowed_paths=["assets"])