Spaces:
Runtime error
Runtime error
Create new file
Browse files
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
import os
|
5 |
+
from pathlib import Path
|
6 |
+
|
7 |
+
title = "💮 BLOOM 💮"
|
8 |
+
description = """Gradio Demo for using BLOOM with Spanish prompts. Heavily based on [Bloom demo](https://huggingface.co/spaces/huggingface/bloom_demo)
|
9 |
+
Tips:
|
10 |
+
- Do NOT talk to BLOOM as an entity, it's not a chatbot but a webpage/blog/article completion model.
|
11 |
+
- For the best results: MIMIC a few sentences of a webpage similar to the content you want to generate.
|
12 |
+
Start a paragraph as if YOU were writing a blog, webpage, math post, coding article and BLOOM will generate a coherent follow-up. Longer prompts usually give more interesting results.
|
13 |
+
Options:
|
14 |
+
- sampling: imaginative completions (may be not super accurate e.g. math/history)
|
15 |
+
- greedy: accurate completions (may be more boring or have repetitions)
|
16 |
+
"""
|
17 |
+
|
18 |
+
API_URL = os.getenv("API_URL")
|
19 |
+
API_TOKEN = os.getenv("API_TOKEN")
|
20 |
+
|
21 |
+
examples = [
|
22 |
+
['Traduce español de España a español de Argentina\nEl coche es rojo - el auto es rojo\nEl ordenador es nuevo - la computadora es nueva\nel boligrafo es negro -', 16, "Sample"],
|
23 |
+
['Estos ejemplos quitan vocales de las palabras\nEjemplos:\nhola - hl\nmanzana - mnzn\npapas - pps\nalacran - lcrn\npapa -', 16, "Sample"],
|
24 |
+
["Un ejemplo de ecuación sencilla sería:\n4x = 40 ; en este caso el valor de x es", 16, "Greedy"],
|
25 |
+
["Si Pedro tiene 4 manzanas y María le quita 2, entonces a Pedro le quedan", 16, "Sample"],
|
26 |
+
["Esta es una conversación entre el modelo de lenguaje BLOOM y uno de sus creadores:\nCreador: Hola, BLOOM! ¿Tienes sentimientos?\nBLOOM:", 32, "Sample"],
|
27 |
+
["Había una vez un circo que alegraba siempre el", 32, "Sample"],
|
28 |
+
['''A continuación se clasifican reseñas de películas:\nComentario: "La película fue un horror"\nEtiqueta: Mala\n\nComentario: "La película me gustó mucho"\nEtiqueta: Buena\n\nComentario: "Es un despropósito de película"\nEtiqueta:''', 16, "Greedy"],
|
29 |
+
['''# La siguiente función hace un petición a la API y devuelve la respuesta en formato JSON\ndef query(payload, model_id, api_token):\n\theaders = {"Authorization": f"Bearer {api_token}"}\n\tAPI_URL = f"https://api-inference.huggingface.co/models/{model_id}"\n\tresponse =''',32, "Sample"],
|
30 |
+
['''Ingredientes de la paella:\n\nArroz bomba - 1500 g\nPollo de corral - 1\nConejo - 0.5 kg\nJudía verde plana''', 32, "Sample"]
|
31 |
+
]
|
32 |
+
|
33 |
+
def query(payload):
|
34 |
+
print(payload)
|
35 |
+
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
36 |
+
response = requests.request("POST", API_URL, headers=headers, json=payload)
|
37 |
+
print(response)
|
38 |
+
return json.loads(response.content.decode("utf-8"))
|
39 |
+
|
40 |
+
def inference(input_sentence, max_length, sample_or_greedy, seed=42):
|
41 |
+
|
42 |
+
if sample_or_greedy == "Sample":
|
43 |
+
parameters = {"max_new_tokens": max_length,
|
44 |
+
"top_p": 0.9,
|
45 |
+
"do_sample": True,
|
46 |
+
"seed": seed,
|
47 |
+
"early_stopping": False,
|
48 |
+
"length_penalty": 0.0,
|
49 |
+
"eos_token_id": None}
|
50 |
+
else:
|
51 |
+
parameters = {"max_new_tokens": max_length,
|
52 |
+
"do_sample": False,
|
53 |
+
"seed": seed,
|
54 |
+
"early_stopping": False,
|
55 |
+
"length_penalty": 0.0,
|
56 |
+
"eos_token_id": None}
|
57 |
+
|
58 |
+
payload = {"inputs": input_sentence,
|
59 |
+
"parameters": parameters}
|
60 |
+
|
61 |
+
data = query(
|
62 |
+
payload
|
63 |
+
)
|
64 |
+
|
65 |
+
print(data)
|
66 |
+
return data[0]['generated_text']
|
67 |
+
|
68 |
+
|
69 |
+
gr.Interface(
|
70 |
+
inference,
|
71 |
+
[
|
72 |
+
gr.inputs.Textbox(label="Input"),
|
73 |
+
gr.inputs.Slider(1, 64, default=32, step=1, label="Tokens to generate"),
|
74 |
+
gr.inputs.Radio(["Sample", "Greedy"], label="Decoding", default="Sample")
|
75 |
+
],
|
76 |
+
["text"],
|
77 |
+
examples=examples,
|
78 |
+
# article=article,
|
79 |
+
cache_examples=False,
|
80 |
+
title=title,
|
81 |
+
description=description
|
82 |
+
).launch()
|