Added fragments metric
Browse files- README.md +8 -1
- app.py +6 -0
- fragments.py +456 -0
- requirements.txt +12 -0
README.md
CHANGED
|
@@ -7,6 +7,13 @@ sdk: gradio
|
|
| 7 |
sdk_version: 3.40.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
| 7 |
sdk_version: 3.40.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
tags:
|
| 11 |
+
- evaluate
|
| 12 |
+
- metric
|
| 13 |
+
description: >-
|
| 14 |
+
Fragments computes the extractiveness between source articles and their summaries. The metric computes two scores: coverage and density.
|
| 15 |
+
The code is adapted from the newsroom package(https://github.com/lil-lab/newsroom/blob/master/newsroom/analyze/fragments.py).
|
| 16 |
+
All credits goes to the authors of aforementioned code.
|
| 17 |
---
|
| 18 |
|
| 19 |
+
|
app.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import evaluate
|
| 2 |
+
from evaluate.utils import launch_gradio_widget
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
module = evaluate.load("fragments", module_type="metric")
|
| 6 |
+
launch_gradio_widget(module)
|
fragments.py
ADDED
|
@@ -0,0 +1,456 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import statistics
|
| 2 |
+
|
| 3 |
+
import evaluate
|
| 4 |
+
import re
|
| 5 |
+
import html as _html
|
| 6 |
+
import itertools as _itertools
|
| 7 |
+
import random as _random
|
| 8 |
+
|
| 9 |
+
from collections import namedtuple as _namedtuple
|
| 10 |
+
|
| 11 |
+
import spacy as _spacy
|
| 12 |
+
from os import system as _system
|
| 13 |
+
|
| 14 |
+
_DESCRIPTION = """\ Fragments computes the extractiveness between source articles and summaries. The metric computes
|
| 15 |
+
two scores: coverage and density. The code is adapted from the newsroom package(
|
| 16 |
+
https://github.com/lil-lab/newsroom/blob/master/newsroom/analyze/fragments.py) and all credits goes to the authors of
|
| 17 |
+
said code."""
|
| 18 |
+
|
| 19 |
+
_KWARGS_DESCRIPTION = """
|
| 20 |
+
Computes coverage and density scores of source articles and their corresponding summaries.
|
| 21 |
+
Args:
|
| 22 |
+
articles (list of str): source articles of the summaries.
|
| 23 |
+
predictions (list of str): list of lists of or just a list of references for each translation.
|
| 24 |
+
language (str): string of which language to use, currently supported are only 'english' and 'german'. Defaults to 'german'
|
| 25 |
+
Returns:
|
| 26 |
+
'coverage': Coverage is the percentage of words in a summary that are from the source article
|
| 27 |
+
'density': Density is the average length of the text spans copied from the document that are contained in the summary.
|
| 28 |
+
Examples:
|
| 29 |
+
|
| 30 |
+
>>> articles = ["This is article 1", "This is article 2"]
|
| 31 |
+
>>> summaries = ["Summary of article 1", "Summary of article 2"]
|
| 32 |
+
>>> fragments = evaluate.load("fragments")
|
| 33 |
+
>>> results = fragments.compute(articles=articles, predictions=summaries)
|
| 34 |
+
>>> print(results["bleu"])
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
|
| 39 |
+
class Fragments(evaluate.Metric):
|
| 40 |
+
def _info(self):
|
| 41 |
+
return evaluate.MetricInfo(
|
| 42 |
+
module_type="metric",
|
| 43 |
+
description=_DESCRIPTION,
|
| 44 |
+
citation=_CITATION,
|
| 45 |
+
inputs_description=_KWARGS_DESCRIPTION,
|
| 46 |
+
features=
|
| 47 |
+
datasets.Features(
|
| 48 |
+
{
|
| 49 |
+
"articles": datasets.Value("string", id="sequence"),
|
| 50 |
+
"predictions": datasets.Value("string", id="sequence"),
|
| 51 |
+
}
|
| 52 |
+
),
|
| 53 |
+
codebase_urls=["https://github.com/lil-lab/newsroom/blob/master/newsroom/analyze/fragments.py"]
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
def _compute(self, articles, predictions, language="german"):
|
| 57 |
+
coverages = []
|
| 58 |
+
densities = []
|
| 59 |
+
for article, summary in zip(articles, predictions):
|
| 60 |
+
fragments = FragmentsOriginal(article, summary, language=language)
|
| 61 |
+
coverages.append(fragments.coverage())
|
| 62 |
+
densities.append(fragments.density())
|
| 63 |
+
|
| 64 |
+
return {
|
| 65 |
+
'coverage': coverages,
|
| 66 |
+
'mean_coverage': statistics.mean(coverages),
|
| 67 |
+
'density': densities,
|
| 68 |
+
'mean_density': statistics.mean(density),
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
class FragmentsOriginal(object):
|
| 73 |
+
Match = _namedtuple("Match", ("summary", "text", "length"))
|
| 74 |
+
|
| 75 |
+
@classmethod
|
| 76 |
+
def _load_model(cls, language):
|
| 77 |
+
|
| 78 |
+
if language == 'english':
|
| 79 |
+
if not hasattr(cls, "_en"):
|
| 80 |
+
|
| 81 |
+
try:
|
| 82 |
+
|
| 83 |
+
cls._en = _spacy.load("en_core_web_sm")
|
| 84 |
+
|
| 85 |
+
except:
|
| 86 |
+
|
| 87 |
+
_system("python -m spacy download en_core_web_sm")
|
| 88 |
+
cls._en = _spacy.load("en_core_web_sm")
|
| 89 |
+
if language == 'german':
|
| 90 |
+
if not hasattr(cls, "_de"):
|
| 91 |
+
|
| 92 |
+
try:
|
| 93 |
+
|
| 94 |
+
cls._de = _spacy.load("de_core_news_sm")
|
| 95 |
+
|
| 96 |
+
except:
|
| 97 |
+
|
| 98 |
+
_system("python -m spacy download de_core_news_sm")
|
| 99 |
+
cls._de = _spacy.load("de_core_news_sm")
|
| 100 |
+
|
| 101 |
+
def __init__(self, text, summary, language="german", tokenize=True, case=False):
|
| 102 |
+
|
| 103 |
+
self._load_model(language)
|
| 104 |
+
|
| 105 |
+
self._tokens = tokenize
|
| 106 |
+
|
| 107 |
+
self.summary = self._tokenize(summary, language) if tokenize else summary.split()
|
| 108 |
+
self.text = self._tokenize(text, language) if tokenize else text.split()
|
| 109 |
+
|
| 110 |
+
self._norm_summary = self._normalize(self.summary, case)
|
| 111 |
+
self._norm_text = self._normalize(self.text, case)
|
| 112 |
+
|
| 113 |
+
self._match(self._norm_summary, self._norm_text)
|
| 114 |
+
|
| 115 |
+
def _tokenize(self, text, language):
|
| 116 |
+
|
| 117 |
+
"""
|
| 118 |
+
Tokenizes input using the fastest possible SpaCy configuration.
|
| 119 |
+
This is optional, can be disabled in constructor.
|
| 120 |
+
"""
|
| 121 |
+
|
| 122 |
+
if language == "english":
|
| 123 |
+
return self._en(text, disable=["tagger", "parser", "ner", "textcat"])
|
| 124 |
+
elif language == "german":
|
| 125 |
+
return self._de(text, disable=["tagger", "parser", "ner", "textcat"])
|
| 126 |
+
else:
|
| 127 |
+
return NotImplementedError
|
| 128 |
+
|
| 129 |
+
def _normalize(self, tokens, case=False):
|
| 130 |
+
|
| 131 |
+
"""
|
| 132 |
+
Lowercases and turns tokens into distinct words.
|
| 133 |
+
"""
|
| 134 |
+
|
| 135 |
+
return [
|
| 136 |
+
str(t).lower()
|
| 137 |
+
if not case
|
| 138 |
+
else str(t)
|
| 139 |
+
for t in tokens
|
| 140 |
+
]
|
| 141 |
+
|
| 142 |
+
def overlaps(self):
|
| 143 |
+
|
| 144 |
+
"""
|
| 145 |
+
Return a list of Fragments.Match objects between summary and text.
|
| 146 |
+
This is a list of named tuples of the form (summary, text, length):
|
| 147 |
+
- summary (int): the start index of the match in the summary
|
| 148 |
+
- text (int): the start index of the match in the reference
|
| 149 |
+
- length (int): the length of the extractive fragment
|
| 150 |
+
"""
|
| 151 |
+
|
| 152 |
+
return self._matches
|
| 153 |
+
|
| 154 |
+
def strings(self, min_length=0, raw=None, summary_base=True):
|
| 155 |
+
|
| 156 |
+
"""
|
| 157 |
+
Return a list of explicit match strings between the summary and reference.
|
| 158 |
+
Note that this will be in the same format as the strings are input. This is
|
| 159 |
+
important to remember if tokenization is done manually. If tokenization is
|
| 160 |
+
specified automatically on the raw strings, raw strings will automatically
|
| 161 |
+
be returned rather than SpaCy tokenized sequences.
|
| 162 |
+
Arguments:
|
| 163 |
+
- min_length (int): filter out overlaps shorter than this (default = 0)
|
| 164 |
+
- raw (bool): return raw input rather than stringified
|
| 165 |
+
- (default = False if automatic tokenization, True otherwise)
|
| 166 |
+
- summary_base (true): strings are based of summary text (default = True)
|
| 167 |
+
Returns:
|
| 168 |
+
- list of overlaps, where overlaps are strings or token sequences
|
| 169 |
+
"""
|
| 170 |
+
|
| 171 |
+
# Compute the strings against the summary or the text?
|
| 172 |
+
|
| 173 |
+
base = self.summary if summary_base else self.text
|
| 174 |
+
|
| 175 |
+
# Generate strings, filtering out strings below the minimum length.
|
| 176 |
+
|
| 177 |
+
strings = [
|
| 178 |
+
base[i: i + length]
|
| 179 |
+
for i, j, length
|
| 180 |
+
in self.overlaps()
|
| 181 |
+
if length > min_length
|
| 182 |
+
]
|
| 183 |
+
|
| 184 |
+
# By default, we just return the tokenization being used.
|
| 185 |
+
# But if they user wants a raw string, then we convert.
|
| 186 |
+
# Mostly, this will be used along with spacy.
|
| 187 |
+
|
| 188 |
+
if self._tokens and raw:
|
| 189 |
+
|
| 190 |
+
for i, s in enumerate(strings):
|
| 191 |
+
strings[i] = str(s)
|
| 192 |
+
|
| 193 |
+
# Return the list of strings.
|
| 194 |
+
|
| 195 |
+
return strings
|
| 196 |
+
|
| 197 |
+
def coverage(self, summary_base=True):
|
| 198 |
+
|
| 199 |
+
"""
|
| 200 |
+
Return the COVERAGE score of the summary and text.
|
| 201 |
+
Arguments:
|
| 202 |
+
- summary_base (bool): use summary as numerator (default = True)
|
| 203 |
+
Returns:
|
| 204 |
+
- decimal COVERAGE score within [0, 1]
|
| 205 |
+
"""
|
| 206 |
+
|
| 207 |
+
numerator = sum(o.length for o in self.overlaps())
|
| 208 |
+
|
| 209 |
+
if summary_base:
|
| 210 |
+
denominator = len(self.summary)
|
| 211 |
+
else:
|
| 212 |
+
denominator = len(self.reference)
|
| 213 |
+
|
| 214 |
+
if denominator == 0:
|
| 215 |
+
return 0
|
| 216 |
+
else:
|
| 217 |
+
return numerator / denominator
|
| 218 |
+
|
| 219 |
+
def density(self, summary_base=True):
|
| 220 |
+
|
| 221 |
+
"""
|
| 222 |
+
Return the DENSITY score of summary and text.
|
| 223 |
+
Arguments:
|
| 224 |
+
- summary_base (bool): use summary as numerator (default = True)
|
| 225 |
+
Returns:
|
| 226 |
+
- decimal DENSITY score within [0, ...]
|
| 227 |
+
"""
|
| 228 |
+
|
| 229 |
+
numerator = sum(o.length ** 2 for o in self.overlaps())
|
| 230 |
+
|
| 231 |
+
if summary_base:
|
| 232 |
+
denominator = len(self.summary)
|
| 233 |
+
else:
|
| 234 |
+
denominator = len(self.reference)
|
| 235 |
+
|
| 236 |
+
if denominator == 0:
|
| 237 |
+
return 0
|
| 238 |
+
else:
|
| 239 |
+
return numerator / denominator
|
| 240 |
+
|
| 241 |
+
def compression(self, text_to_summary=True):
|
| 242 |
+
|
| 243 |
+
"""
|
| 244 |
+
Return compression ratio between summary and text.
|
| 245 |
+
Arguments:
|
| 246 |
+
- text_to_summary (bool): compute text/summary ratio (default = True)
|
| 247 |
+
Returns:
|
| 248 |
+
- decimal compression score within [0, ...]
|
| 249 |
+
"""
|
| 250 |
+
|
| 251 |
+
ratio = [len(self.text), len(self.summary)]
|
| 252 |
+
|
| 253 |
+
try:
|
| 254 |
+
|
| 255 |
+
if text_to_summary:
|
| 256 |
+
return ratio[0] / ratio[1]
|
| 257 |
+
else:
|
| 258 |
+
return ratio[1] / ratio[0]
|
| 259 |
+
|
| 260 |
+
except ZeroDivisionError:
|
| 261 |
+
|
| 262 |
+
return 0
|
| 263 |
+
|
| 264 |
+
def _match(self, a, b):
|
| 265 |
+
|
| 266 |
+
"""
|
| 267 |
+
Raw procedure for matching summary in text, described in paper.
|
| 268 |
+
"""
|
| 269 |
+
|
| 270 |
+
self._matches = []
|
| 271 |
+
|
| 272 |
+
a_start = b_start = 0
|
| 273 |
+
|
| 274 |
+
while a_start < len(a):
|
| 275 |
+
|
| 276 |
+
best_match = None
|
| 277 |
+
best_match_length = 0
|
| 278 |
+
|
| 279 |
+
while b_start < len(b):
|
| 280 |
+
|
| 281 |
+
if a[a_start] == b[b_start]:
|
| 282 |
+
|
| 283 |
+
a_end = a_start
|
| 284 |
+
b_end = b_start
|
| 285 |
+
|
| 286 |
+
while a_end < len(a) and b_end < len(b) \
|
| 287 |
+
and b[b_end] == a[a_end]:
|
| 288 |
+
b_end += 1
|
| 289 |
+
a_end += 1
|
| 290 |
+
|
| 291 |
+
length = a_end - a_start
|
| 292 |
+
|
| 293 |
+
if length > best_match_length:
|
| 294 |
+
best_match = Fragments.Match(a_start, b_start, length)
|
| 295 |
+
best_match_length = length
|
| 296 |
+
|
| 297 |
+
b_start = b_end
|
| 298 |
+
|
| 299 |
+
else:
|
| 300 |
+
|
| 301 |
+
b_start += 1
|
| 302 |
+
|
| 303 |
+
b_start = 0
|
| 304 |
+
|
| 305 |
+
if best_match:
|
| 306 |
+
|
| 307 |
+
if best_match_length > 0:
|
| 308 |
+
self._matches.append(best_match)
|
| 309 |
+
|
| 310 |
+
a_start += best_match_length
|
| 311 |
+
|
| 312 |
+
else:
|
| 313 |
+
|
| 314 |
+
a_start += 1
|
| 315 |
+
|
| 316 |
+
def _htmltokens(self, tokens):
|
| 317 |
+
|
| 318 |
+
"""
|
| 319 |
+
Carefully process tokens to handle whitespace and HTML characters.
|
| 320 |
+
"""
|
| 321 |
+
|
| 322 |
+
return [
|
| 323 |
+
[
|
| 324 |
+
_html.escape(t.text).replace("\n", "<br/>"),
|
| 325 |
+
_html.escape(t.whitespace_).replace("\n", "<br/>")
|
| 326 |
+
]
|
| 327 |
+
|
| 328 |
+
for t in tokens
|
| 329 |
+
]
|
| 330 |
+
|
| 331 |
+
def annotate(self, min_length=0, text_truncation=None, novel_italics=False):
|
| 332 |
+
|
| 333 |
+
"""
|
| 334 |
+
Used to annotate fragments for website visualization.
|
| 335 |
+
Arguments:
|
| 336 |
+
- min_length (int): minimum length overlap to count (default = 0)
|
| 337 |
+
- text_truncation (int): tuncated text length (default = None)
|
| 338 |
+
- novel_italics (bool): italicize novel words (default = True)
|
| 339 |
+
Returns:
|
| 340 |
+
- a tuple of strings: (summary HTML, text HTML)
|
| 341 |
+
"""
|
| 342 |
+
|
| 343 |
+
start = """
|
| 344 |
+
<u
|
| 345 |
+
style="color: {color}; border-color: {color};"
|
| 346 |
+
data-ref="{ref}" title="Length: {length}"
|
| 347 |
+
>
|
| 348 |
+
""".strip()
|
| 349 |
+
|
| 350 |
+
end = """
|
| 351 |
+
</u>
|
| 352 |
+
""".strip()
|
| 353 |
+
|
| 354 |
+
# Here we tokenize carefully to preserve sane-looking whitespace.
|
| 355 |
+
# (This part does require text to use a SpaCy tokenization.)
|
| 356 |
+
|
| 357 |
+
summary = self._htmltokens(self.summary)
|
| 358 |
+
text = self._htmltokens(self.text)
|
| 359 |
+
|
| 360 |
+
# Compute novel word set, if requested.
|
| 361 |
+
|
| 362 |
+
if novel_italics:
|
| 363 |
+
|
| 364 |
+
novel = set(self._norm_summary) - set(self._norm_text)
|
| 365 |
+
|
| 366 |
+
for word_whitespace in summary:
|
| 367 |
+
|
| 368 |
+
if word_whitespace[0].lower() in novel:
|
| 369 |
+
word_whitespace[0] = "<em>" + word_whitespace[0] + "</em>"
|
| 370 |
+
|
| 371 |
+
# Truncate text, if requested.
|
| 372 |
+
# Must be careful later on with this.
|
| 373 |
+
|
| 374 |
+
if text_truncation is not None:
|
| 375 |
+
text = text[:text_truncation]
|
| 376 |
+
|
| 377 |
+
# March through overlaps, replacing tokens with HTML-tagged strings.
|
| 378 |
+
|
| 379 |
+
colors = self._itercolors()
|
| 380 |
+
|
| 381 |
+
for overlap in self.overlaps():
|
| 382 |
+
|
| 383 |
+
# Skip overlaps that are too short.
|
| 384 |
+
|
| 385 |
+
if overlap.length < min_length:
|
| 386 |
+
continue
|
| 387 |
+
|
| 388 |
+
# Reference ID for JavaScript highlighting.
|
| 389 |
+
# This is random, but shared between corresponding fragments.
|
| 390 |
+
|
| 391 |
+
ref = _random.randint(0, 1e10)
|
| 392 |
+
color = next(colors)
|
| 393 |
+
|
| 394 |
+
# Summary starting tag.
|
| 395 |
+
|
| 396 |
+
summary[overlap.summary][0] = start.format(
|
| 397 |
+
color=color,
|
| 398 |
+
ref=ref,
|
| 399 |
+
length=overlap.length,
|
| 400 |
+
) + summary[overlap.summary][0]
|
| 401 |
+
|
| 402 |
+
# Text starting tag.
|
| 403 |
+
|
| 404 |
+
text[overlap.text][0] = start.format(
|
| 405 |
+
color=color,
|
| 406 |
+
ref=ref,
|
| 407 |
+
length=overlap.length,
|
| 408 |
+
) + text[overlap.text][0]
|
| 409 |
+
|
| 410 |
+
# Summary ending tag.
|
| 411 |
+
|
| 412 |
+
summary[overlap.summary + overlap.length - 1][0] += end
|
| 413 |
+
|
| 414 |
+
# Text ending tag.
|
| 415 |
+
|
| 416 |
+
text[overlap.text + overlap.length - 1][0] += end
|
| 417 |
+
|
| 418 |
+
# Carefully join tokens and whitespace to reconstruct the string.
|
| 419 |
+
|
| 420 |
+
summary = " ".join("".join("".join(tw) for tw in summary).split())
|
| 421 |
+
text = " ".join("".join("".join(tw) for tw in text).split())
|
| 422 |
+
|
| 423 |
+
# Return the tuple.
|
| 424 |
+
|
| 425 |
+
return summary, text
|
| 426 |
+
|
| 427 |
+
def _itercolors(self):
|
| 428 |
+
|
| 429 |
+
# Endlessly cycle through these colors.
|
| 430 |
+
|
| 431 |
+
return _itertools.cycle((
|
| 432 |
+
|
| 433 |
+
"#393b79",
|
| 434 |
+
"#5254a3",
|
| 435 |
+
"#6b6ecf",
|
| 436 |
+
"#9c9ede",
|
| 437 |
+
"#637939",
|
| 438 |
+
"#8ca252",
|
| 439 |
+
"#b5cf6b",
|
| 440 |
+
"#cedb9c",
|
| 441 |
+
"#8c6d31",
|
| 442 |
+
"#bd9e39",
|
| 443 |
+
"#e7ba52",
|
| 444 |
+
"#e7cb94",
|
| 445 |
+
"#843c39",
|
| 446 |
+
"#ad494a",
|
| 447 |
+
"#d6616b",
|
| 448 |
+
"#e7969c",
|
| 449 |
+
"#7b4173",
|
| 450 |
+
"#a55194",
|
| 451 |
+
"#ce6dbd",
|
| 452 |
+
"#de9ed6",
|
| 453 |
+
|
| 454 |
+
))
|
| 455 |
+
|
| 456 |
+
################################################################################
|
requirements.txt
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Fragments
|
| 3 |
+
emoji: 🌍
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: red
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 3.40.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
|