hysts HF Staff commited on
Commit
0314abc
·
1 Parent(s): 754973e
Files changed (11) hide show
  1. .pre-commit-config.yaml +33 -0
  2. .python-version +1 -0
  3. .vscode/extensions.json +8 -0
  4. .vscode/settings.json +17 -0
  5. LICENSE +21 -0
  6. README.md +4 -4
  7. app.py +62 -0
  8. pyproject.toml +52 -0
  9. requirements.txt +161 -0
  10. style.css +11 -0
  11. uv.lock +0 -0
.pre-commit-config.yaml ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ repos:
2
+ - repo: https://github.com/pre-commit/pre-commit-hooks
3
+ rev: v5.0.0
4
+ hooks:
5
+ - id: check-executables-have-shebangs
6
+ - id: check-json
7
+ - id: check-merge-conflict
8
+ - id: check-shebang-scripts-are-executable
9
+ - id: check-toml
10
+ - id: check-yaml
11
+ - id: end-of-file-fixer
12
+ - id: mixed-line-ending
13
+ args: ["--fix=lf"]
14
+ - id: requirements-txt-fixer
15
+ - id: trailing-whitespace
16
+ - repo: https://github.com/astral-sh/ruff-pre-commit
17
+ rev: v0.9.7
18
+ hooks:
19
+ - id: ruff
20
+ args: ["--fix"]
21
+ - id: ruff-format
22
+ - repo: https://github.com/pre-commit/mirrors-mypy
23
+ rev: v1.15.0
24
+ hooks:
25
+ - id: mypy
26
+ args: ["--ignore-missing-imports"]
27
+ additional_dependencies:
28
+ [
29
+ "types-python-slugify",
30
+ "types-pytz",
31
+ "types-PyYAML",
32
+ "types-requests",
33
+ ]
.python-version ADDED
@@ -0,0 +1 @@
 
 
1
+ 3.10
.vscode/extensions.json ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "recommendations": [
3
+ "ms-python.python",
4
+ "charliermarsh.ruff",
5
+ "streetsidesoftware.code-spell-checker",
6
+ "tamasfe.even-better-toml"
7
+ ]
8
+ }
.vscode/settings.json ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "editor.formatOnSave": true,
3
+ "files.insertFinalNewline": false,
4
+ "[python]": {
5
+ "editor.defaultFormatter": "charliermarsh.ruff",
6
+ "editor.formatOnType": true,
7
+ "editor.codeActionsOnSave": {
8
+ "source.fixAll.ruff": "explicit",
9
+ "source.organizeImports": "explicit"
10
+ }
11
+ },
12
+ "[jupyter]": {
13
+ "files.insertFinalNewline": false
14
+ },
15
+ "notebook.output.scrolling": true,
16
+ "notebook.formatOnSave.enabled": true
17
+ }
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2023 hysts
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
- title: Claude 3 7 Sample
3
- emoji: 🌖
4
- colorFrom: purple
5
- colorTo: blue
6
  sdk: gradio
7
  sdk_version: 5.18.0
8
  app_file: app.py
 
1
  ---
2
+ title: Claude 3.7 Sample
3
+ emoji:
4
+ colorFrom: red
5
+ colorTo: purple
6
  sdk: gradio
7
  sdk_version: 5.18.0
8
  app_file: app.py
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ import os
4
+ import time
5
+ from collections.abc import Iterator
6
+
7
+ import anthropic
8
+ import gradio as gr
9
+ from gradio import ChatMessage
10
+
11
+ client = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
12
+
13
+
14
+ def fn(
15
+ message: str, history: list[dict], max_tokens: int, thinking_budget: int
16
+ ) -> Iterator[ChatMessage | list[ChatMessage]]:
17
+ messages = []
18
+ for past_message in history:
19
+ if past_message["role"] == "assistant" and past_message["metadata"]:
20
+ continue
21
+ messages.append({"role": past_message["role"], "content": past_message["content"]})
22
+ messages = [*messages, {"role": "user", "content": message}]
23
+
24
+ with client.messages.stream(
25
+ model="claude-3-7-sonnet-20250219",
26
+ max_tokens=max_tokens,
27
+ thinking={"type": "enabled", "budget_tokens": thinking_budget},
28
+ messages=messages,
29
+ ) as stream:
30
+ for event in stream:
31
+ if event.type == "content_block_start":
32
+ if event.content_block.type == "thinking":
33
+ start_time = time.perf_counter()
34
+ thought = ChatMessage(content="", metadata={"title": "Thinking", "status": "pending"})
35
+ yield thought
36
+ else:
37
+ response = ChatMessage(content="")
38
+ elif event.type == "content_block_delta":
39
+ if event.delta.type == "thinking_delta":
40
+ thought.content += event.delta.thinking
41
+ yield thought
42
+ elif event.delta.type == "text_delta":
43
+ response.content += event.delta.text
44
+ yield [thought, response]
45
+ elif event.type == "content_block_stop" and event.content_block.type == "thinking":
46
+ thought.metadata["status"] = "done"
47
+ thought.metadata["duration"] = time.perf_counter() - start_time
48
+ yield thought
49
+
50
+
51
+ demo = gr.ChatInterface(
52
+ fn=fn,
53
+ type="messages",
54
+ additional_inputs=[
55
+ gr.Slider(label="Max Tokens", minimum=1024, maximum=128000, step=1, value=10000),
56
+ gr.Slider(label="Thinking Budget", minimum=1024, maximum=128000, step=1, value=8000),
57
+ ],
58
+ chatbot=gr.Chatbot(type="messages", scale=1, show_copy_button=True),
59
+ )
60
+
61
+ if __name__ == "__main__":
62
+ demo.launch()
pyproject.toml ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "claude-3-7-sample"
3
+ version = "0.1.0"
4
+ description = ""
5
+ readme = "README.md"
6
+ requires-python = ">=3.10"
7
+ dependencies = [
8
+ "anthropic>=0.47.2",
9
+ "gradio>=5.18.0",
10
+ ]
11
+
12
+ [tool.ruff]
13
+ line-length = 119
14
+
15
+ [tool.ruff.lint]
16
+ select = ["ALL"]
17
+ ignore = [
18
+ "COM812", # missing-trailing-comma
19
+ "D203", # one-blank-line-before-class
20
+ "D213", # multi-line-summary-second-line
21
+ "E501", # line-too-long
22
+ "SIM117", # multiple-with-statements
23
+ ]
24
+ extend-ignore = [
25
+ "D100", # undocumented-public-module
26
+ "D101", # undocumented-public-class
27
+ "D102", # undocumented-public-method
28
+ "D103", # undocumented-public-function
29
+ "D104", # undocumented-public-package
30
+ "D105", # undocumented-magic-method
31
+ "D107", # undocumented-public-init
32
+ "EM101", # raw-string-in-exception
33
+ "FBT001", # boolean-type-hint-positional-argument
34
+ "FBT002", # boolean-default-value-positional-argument
35
+ "PD901", # pandas-df-variable-name
36
+ "PGH003", # blanket-type-ignore
37
+ "PLR0913", # too-many-arguments
38
+ "PLR0915", # too-many-statements
39
+ "TRY003", # raise-vanilla-args
40
+ ]
41
+ unfixable = [
42
+ "F401", # unused-import
43
+ ]
44
+
45
+ [tool.ruff.lint.pydocstyle]
46
+ convention = "google"
47
+
48
+ [tool.ruff.lint.per-file-ignores]
49
+ "*.ipynb" = ["T201", "T203"]
50
+
51
+ [tool.ruff.format]
52
+ docstring-code-format = true
requirements.txt ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This file was autogenerated by uv via the following command:
2
+ # uv pip compile pyproject.toml -o requirements.txt
3
+ aiofiles==23.2.1
4
+ # via gradio
5
+ annotated-types==0.7.0
6
+ # via pydantic
7
+ anthropic==0.47.2
8
+ # via claude-3-7-sample (pyproject.toml)
9
+ anyio==4.8.0
10
+ # via
11
+ # anthropic
12
+ # gradio
13
+ # httpx
14
+ # starlette
15
+ certifi==2025.1.31
16
+ # via
17
+ # httpcore
18
+ # httpx
19
+ # requests
20
+ charset-normalizer==3.4.1
21
+ # via requests
22
+ click==8.1.8
23
+ # via
24
+ # typer
25
+ # uvicorn
26
+ distro==1.9.0
27
+ # via anthropic
28
+ exceptiongroup==1.2.2
29
+ # via anyio
30
+ fastapi==0.115.8
31
+ # via gradio
32
+ ffmpy==0.5.0
33
+ # via gradio
34
+ filelock==3.17.0
35
+ # via huggingface-hub
36
+ fsspec==2025.2.0
37
+ # via
38
+ # gradio-client
39
+ # huggingface-hub
40
+ gradio==5.18.0
41
+ # via claude-3-7-sample (pyproject.toml)
42
+ gradio-client==1.7.2
43
+ # via gradio
44
+ h11==0.14.0
45
+ # via
46
+ # httpcore
47
+ # uvicorn
48
+ httpcore==1.0.7
49
+ # via httpx
50
+ httpx==0.28.1
51
+ # via
52
+ # anthropic
53
+ # gradio
54
+ # gradio-client
55
+ # safehttpx
56
+ huggingface-hub==0.29.1
57
+ # via
58
+ # gradio
59
+ # gradio-client
60
+ idna==3.10
61
+ # via
62
+ # anyio
63
+ # httpx
64
+ # requests
65
+ jinja2==3.1.5
66
+ # via gradio
67
+ jiter==0.8.2
68
+ # via anthropic
69
+ markdown-it-py==3.0.0
70
+ # via rich
71
+ markupsafe==2.1.5
72
+ # via
73
+ # gradio
74
+ # jinja2
75
+ mdurl==0.1.2
76
+ # via markdown-it-py
77
+ numpy==2.2.3
78
+ # via
79
+ # gradio
80
+ # pandas
81
+ orjson==3.10.15
82
+ # via gradio
83
+ packaging==24.2
84
+ # via
85
+ # gradio
86
+ # gradio-client
87
+ # huggingface-hub
88
+ pandas==2.2.3
89
+ # via gradio
90
+ pillow==11.1.0
91
+ # via gradio
92
+ pydantic==2.10.6
93
+ # via
94
+ # anthropic
95
+ # fastapi
96
+ # gradio
97
+ pydantic-core==2.27.2
98
+ # via pydantic
99
+ pydub==0.25.1
100
+ # via gradio
101
+ pygments==2.19.1
102
+ # via rich
103
+ python-dateutil==2.9.0.post0
104
+ # via pandas
105
+ python-multipart==0.0.20
106
+ # via gradio
107
+ pytz==2025.1
108
+ # via pandas
109
+ pyyaml==6.0.2
110
+ # via
111
+ # gradio
112
+ # huggingface-hub
113
+ requests==2.32.3
114
+ # via huggingface-hub
115
+ rich==13.9.4
116
+ # via typer
117
+ ruff==0.9.7
118
+ # via gradio
119
+ safehttpx==0.1.6
120
+ # via gradio
121
+ semantic-version==2.10.0
122
+ # via gradio
123
+ shellingham==1.5.4
124
+ # via typer
125
+ six==1.17.0
126
+ # via python-dateutil
127
+ sniffio==1.3.1
128
+ # via
129
+ # anthropic
130
+ # anyio
131
+ starlette==0.45.3
132
+ # via
133
+ # fastapi
134
+ # gradio
135
+ tomlkit==0.13.2
136
+ # via gradio
137
+ tqdm==4.67.1
138
+ # via huggingface-hub
139
+ typer==0.15.1
140
+ # via gradio
141
+ typing-extensions==4.12.2
142
+ # via
143
+ # anthropic
144
+ # anyio
145
+ # fastapi
146
+ # gradio
147
+ # gradio-client
148
+ # huggingface-hub
149
+ # pydantic
150
+ # pydantic-core
151
+ # rich
152
+ # typer
153
+ # uvicorn
154
+ tzdata==2025.1
155
+ # via pandas
156
+ urllib3==2.3.0
157
+ # via requests
158
+ uvicorn==0.34.0
159
+ # via gradio
160
+ websockets==15.0
161
+ # via gradio-client
style.css ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ h1 {
2
+ text-align: center;
3
+ display: block;
4
+ }
5
+
6
+ #duplicate-button {
7
+ margin: auto;
8
+ color: #fff;
9
+ background: #1565c0;
10
+ border-radius: 100vh;
11
+ }
uv.lock ADDED
The diff for this file is too large to render. See raw diff