Alejandro Rodriguez Salamanca commited on
Commit
251d97b
·
1 Parent(s): c7fe634
Files changed (8) hide show
  1. .gitignore +1 -0
  2. .python-version +1 -0
  3. README.md +2 -0
  4. app.py +144 -0
  5. pyproject.toml +51 -0
  6. requirements.txt +183 -0
  7. style.css +10 -0
  8. uv.lock +0 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ __pycache__/
.python-version ADDED
@@ -0,0 +1 @@
 
 
1
+ 3.12
README.md CHANGED
@@ -7,6 +7,8 @@ sdk: gradio
7
  sdk_version: 6.5.1
8
  app_file: app.py
9
  pinned: false
 
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
7
  sdk_version: 6.5.1
8
  app_file: app.py
9
  pinned: false
10
+ models:
11
+ - CohereLabs/tiny-aya-global
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import os
3
+ from collections.abc import Iterator
4
+
5
+ import gradio as gr
6
+ from cohere import ClientV2
7
+
8
+ logger = logging.getLogger(__name__)
9
+
10
+ model_id = "tiny-aya-global"
11
+
12
+ # Initialize Cohere client
13
+ api_key = os.getenv("COHERE_API_KEY")
14
+ if not api_key:
15
+ raise ValueError("COHERE_API_KEY environment variable is required")
16
+ client = ClientV2(api_key=api_key, client_name="hf-tiny-aya-global")
17
+
18
+
19
+ def generate(
20
+ message: str,
21
+ history: list[dict],
22
+ system_prompt: str = "",
23
+ temperature: float = 0.3,
24
+ max_new_tokens: int = 700,
25
+ ) -> Iterator[str]:
26
+ """Stream a response from the Cohere API for the given message and conversation history."""
27
+ messages: list[dict[str, str]] = []
28
+ system_prompt = (system_prompt or "").strip()
29
+ if system_prompt:
30
+ messages.append({"role": "system", "content": system_prompt})
31
+
32
+ # Add conversation history
33
+ for item in history:
34
+ role = item.get("role")
35
+ content = item.get("content") or ""
36
+ if not isinstance(content, str):
37
+ content = str(content)
38
+ if role in ("assistant", "user") and content:
39
+ messages.append({"role": role, "content": content})
40
+
41
+ # Add current user message
42
+ current_text = str(message or "").strip()
43
+ if not current_text:
44
+ yield ""
45
+ return
46
+ messages.append({"role": "user", "content": current_text})
47
+
48
+ try:
49
+ # Call Cohere API using the correct event type and delta access
50
+ response = client.chat_stream(
51
+ model=model_id,
52
+ messages=messages,
53
+ temperature=temperature,
54
+ max_tokens=max_new_tokens,
55
+ )
56
+
57
+ output = ""
58
+ for event in response:
59
+ if getattr(event, "type", None) == "content-delta":
60
+ # event.delta.message.content.text is the streamed text
61
+ text = getattr(event.delta.message.content, "text", "")
62
+ output += text
63
+ yield output
64
+
65
+ except Exception:
66
+ logger.exception("Cohere API error")
67
+ gr.Warning("Something went wrong generating a response. Please try again.")
68
+ yield ""
69
+
70
+
71
+ examples = [
72
+ ["Explica en español qué significa la palabra japonesa 'ikigai' y da un ejemplo práctico."],
73
+ ["اكتب فقرة قصيرة تصف غروب الشمس في الصحراء"],
74
+ ["Kwa nini ni muhimu kujifunza lugha zaidi ya moja? Toa sababu tatu."],
75
+ ["一个从未见过大海的山村孩子,第一次来到海边。用三到五句话描述他的感受。"],
76
+ [
77
+ "Translate the following sentence from Basque to French: "
78
+ "'Hizkuntza-eredu handiek milioika testu erabiltzen dituzte ikasteko, "
79
+ "baina hizkuntza txikientzat datu gutxiago dago.'"
80
+ ],
81
+ [
82
+ "ਜੇਕਰ ਕੋਈ ਵਿਅਕਤੀ ਪਹਿਲੀ ਵਾਰ ਵਿਦੇਸ਼ ਜਾ ਰਿਹਾ ਹੈ, ਤਾਂ ਉਸ ਨੂੰ ਕਿਹੜੀਆਂ ਗੱਲਾਂ ਦਾ "
83
+ "ਧਿਆਨ ਰੱਖਣਾ ਚਾਹੀਦਾ ਹੈ? ਪੰਜ ਸੁਝਾਅ ਦਿਓ।"
84
+ ],
85
+ [
86
+ "Eglurwch mewn tair brawddeg pam mae bioamrywiaeth yn bwysig i ecosystemau."
87
+ ],
88
+ [
89
+ "ถ้าคุณต้องการเริ่มต้นออกกำลังกายเป็นประจำ ควรเริ่มต้นอย่างไร? ให้คำแนะนำสามข้อ"
90
+ ]
91
+ ]
92
+
93
+ example_labels = [
94
+ "Spanish — Explain 'ikigai'",
95
+ "Arabic — Describe a desert sunset",
96
+ "Swahili — Why learn multiple languages?",
97
+ "Chinese — A child sees the ocean",
98
+ "English — Basque to French translation",
99
+ "Punjabi — Travel tips for first-timers",
100
+ "Welsh — Why biodiversity is important to ecosystems",
101
+ "Thai — How to start exercising regularly",
102
+ ]
103
+
104
+ DESCRIPTION = (
105
+ "**[Tiny Aya](https://huggingface.co/CohereLabs/tiny-aya-global)** is a lightweight "
106
+ "multilingual language model by [Cohere Labs](https://cohere.com/research). "
107
+ "Try chatting in any of 70+ supported languages!"
108
+ )
109
+
110
+ demo = gr.ChatInterface(
111
+ fn=generate,
112
+ chatbot=gr.Chatbot(min_height=600),
113
+ textbox=gr.Textbox(
114
+ autofocus=True,
115
+ placeholder="Type a message in any language... / Escribe en cualquier idioma... / أكتب بأي لغة...",
116
+ ),
117
+ additional_inputs=[
118
+ gr.Textbox(
119
+ label="System Prompt (optional)",
120
+ placeholder="e.g. You are a helpful multilingual assistant. Always respond in the user's language.",
121
+ lines=3,
122
+ ),
123
+ gr.Slider(label="Temperature", minimum=0.0, maximum=1.0, step=0.05, value=0.3),
124
+ gr.Slider(label="Max New Tokens", minimum=100, maximum=2000, step=10, value=700),
125
+ ],
126
+ stop_btn=False,
127
+ title="Tiny Aya",
128
+ description=DESCRIPTION,
129
+ examples=examples,
130
+ example_labels=example_labels,
131
+ run_examples_on_click=True,
132
+ cache_examples=False,
133
+ delete_cache=(1800, 1800),
134
+ save_history=True,
135
+ )
136
+
137
+ if __name__ == "__main__":
138
+ demo.launch(
139
+ theme=gr.themes.Soft(
140
+ primary_hue="green",
141
+ font=[gr.themes.GoogleFont("Inter"), "system-ui", "sans-serif"],
142
+ ),
143
+ css_paths="style.css",
144
+ )
pyproject.toml ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "tiny-aya-hf-space"
3
+ version = "0.1.0"
4
+ description = "Tiny Aya HF Space"
5
+ readme = "README.md"
6
+ requires-python = ">=3.12"
7
+ dependencies = [
8
+ "cohere>=5.16.1",
9
+ "gradio>=6.5.1",
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
+ "D100", # undocumented-public-module
25
+ "D101", # undocumented-public-class
26
+ "D102", # undocumented-public-method
27
+ "D103", # undocumented-public-function
28
+ "D104", # undocumented-public-package
29
+ "D105", # undocumented-magic-method
30
+ "D107", # undocumented-public-init
31
+ "EM101", # raw-string-in-exception
32
+ "FBT001", # boolean-type-hint-positional-argument
33
+ "FBT002", # boolean-default-value-positional-argument
34
+ "PD901", # pandas-df-variable-name
35
+ "PGH003", # blanket-type-ignore
36
+ "PLR0913", # too-many-arguments
37
+ "PLR0915", # too-many-statements
38
+ "TRY003", # raise-vanilla-args
39
+ ]
40
+ unfixable = [
41
+ "F401", # unused-import
42
+ ]
43
+
44
+ [tool.ruff.lint.pydocstyle]
45
+ convention = "google"
46
+
47
+ [tool.ruff.lint.per-file-ignores]
48
+ "*.ipynb" = ["T201", "T203"]
49
+
50
+ [tool.ruff.format]
51
+ docstring-code-format = true
requirements.txt ADDED
@@ -0,0 +1,183 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This file was autogenerated by uv via the following command:
2
+ # uv export --no-hashes --format requirements-txt
3
+ aiofiles==24.1.0
4
+ # via gradio
5
+ annotated-doc==0.0.4
6
+ # via
7
+ # fastapi
8
+ # typer
9
+ annotated-types==0.7.0
10
+ # via pydantic
11
+ anyio==4.12.1
12
+ # via
13
+ # gradio
14
+ # httpx
15
+ # starlette
16
+ audioop-lts==0.2.2 ; python_full_version >= '3.13'
17
+ # via gradio
18
+ brotli==1.2.0
19
+ # via gradio
20
+ certifi==2026.1.4
21
+ # via
22
+ # httpcore
23
+ # httpx
24
+ # requests
25
+ charset-normalizer==3.4.4
26
+ # via requests
27
+ click==8.3.1
28
+ # via
29
+ # typer
30
+ # uvicorn
31
+ cohere==5.20.5
32
+ # via tiny-aya-hf-space
33
+ colorama==0.4.6 ; sys_platform == 'win32'
34
+ # via
35
+ # click
36
+ # tqdm
37
+ fastapi==0.129.0
38
+ # via gradio
39
+ fastavro==1.12.1
40
+ # via cohere
41
+ ffmpy==1.0.0
42
+ # via gradio
43
+ filelock==3.21.2
44
+ # via huggingface-hub
45
+ fsspec==2026.2.0
46
+ # via
47
+ # gradio-client
48
+ # huggingface-hub
49
+ gradio==6.5.1
50
+ # via tiny-aya-hf-space
51
+ gradio-client==2.0.3
52
+ # via gradio
53
+ groovy==0.1.2
54
+ # via gradio
55
+ h11==0.16.0
56
+ # via
57
+ # httpcore
58
+ # uvicorn
59
+ hf-xet==1.2.0 ; platform_machine == 'AMD64' or platform_machine == 'aarch64' or platform_machine == 'amd64' or platform_machine == 'arm64' or platform_machine == 'x86_64'
60
+ # via huggingface-hub
61
+ httpcore==1.0.9
62
+ # via httpx
63
+ httpx==0.28.1
64
+ # via
65
+ # cohere
66
+ # gradio
67
+ # gradio-client
68
+ # huggingface-hub
69
+ # safehttpx
70
+ huggingface-hub==1.4.1
71
+ # via
72
+ # gradio
73
+ # gradio-client
74
+ # tokenizers
75
+ idna==3.11
76
+ # via
77
+ # anyio
78
+ # httpx
79
+ # requests
80
+ jinja2==3.1.6
81
+ # via gradio
82
+ markdown-it-py==4.0.0
83
+ # via rich
84
+ markupsafe==3.0.3
85
+ # via
86
+ # gradio
87
+ # jinja2
88
+ mdurl==0.1.2
89
+ # via markdown-it-py
90
+ numpy==2.4.2
91
+ # via
92
+ # gradio
93
+ # pandas
94
+ orjson==3.11.7
95
+ # via gradio
96
+ packaging==26.0
97
+ # via
98
+ # gradio
99
+ # gradio-client
100
+ # huggingface-hub
101
+ pandas==3.0.0
102
+ # via gradio
103
+ pillow==12.1.1
104
+ # via gradio
105
+ pydantic==2.12.5
106
+ # via
107
+ # cohere
108
+ # fastapi
109
+ # gradio
110
+ pydantic-core==2.41.5
111
+ # via
112
+ # cohere
113
+ # pydantic
114
+ pydub==0.25.1
115
+ # via gradio
116
+ pygments==2.19.2
117
+ # via rich
118
+ python-dateutil==2.9.0.post0
119
+ # via pandas
120
+ python-multipart==0.0.22
121
+ # via gradio
122
+ pytz==2025.2
123
+ # via gradio
124
+ pyyaml==6.0.3
125
+ # via
126
+ # gradio
127
+ # huggingface-hub
128
+ requests==2.32.5
129
+ # via cohere
130
+ rich==14.3.2
131
+ # via typer
132
+ safehttpx==0.1.7
133
+ # via gradio
134
+ semantic-version==2.10.0
135
+ # via gradio
136
+ shellingham==1.5.4
137
+ # via
138
+ # huggingface-hub
139
+ # typer
140
+ six==1.17.0
141
+ # via python-dateutil
142
+ starlette==0.52.1
143
+ # via
144
+ # fastapi
145
+ # gradio
146
+ tokenizers==0.22.2
147
+ # via cohere
148
+ tomlkit==0.13.3
149
+ # via gradio
150
+ tqdm==4.67.3
151
+ # via huggingface-hub
152
+ typer==0.23.1
153
+ # via
154
+ # gradio
155
+ # typer-slim
156
+ typer-slim==0.23.1
157
+ # via huggingface-hub
158
+ types-requests==2.32.4.20260107
159
+ # via cohere
160
+ typing-extensions==4.15.0
161
+ # via
162
+ # anyio
163
+ # cohere
164
+ # fastapi
165
+ # gradio
166
+ # gradio-client
167
+ # huggingface-hub
168
+ # pydantic
169
+ # pydantic-core
170
+ # starlette
171
+ # typing-inspection
172
+ typing-inspection==0.4.2
173
+ # via
174
+ # fastapi
175
+ # pydantic
176
+ tzdata==2025.3 ; sys_platform == 'emscripten' or sys_platform == 'win32'
177
+ # via pandas
178
+ urllib3==2.6.3
179
+ # via
180
+ # requests
181
+ # types-requests
182
+ uvicorn==0.40.0
183
+ # via gradio
style.css ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Center the title */
2
+ h1 {
3
+ text-align: center;
4
+ display: block;
5
+ }
6
+
7
+ /* Center the description below the title */
8
+ .prose {
9
+ text-align: center;
10
+ }
uv.lock ADDED
The diff for this file is too large to render. See raw diff