Spaces:
Runtime error
Runtime error
Update text_generator.py
Browse files- text_generator.py +25 -58
text_generator.py
CHANGED
|
@@ -1,67 +1,34 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from transformers import Tool
|
| 7 |
-
# Import other necessary libraries if needed
|
| 8 |
|
| 9 |
-
class
|
| 10 |
name = "text_generator"
|
| 11 |
-
description =
|
| 12 |
-
"This is a tool for text generation. It takes a prompt as input and returns the generated text."
|
| 13 |
-
)
|
| 14 |
-
|
| 15 |
inputs = ["text"]
|
| 16 |
outputs = ["text"]
|
| 17 |
|
| 18 |
def __call__(self, prompt: str):
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
#}
|
| 25 |
-
|
| 26 |
-
#payload = {
|
| 27 |
-
# "inputs": "Can you please let us know more details about your ",
|
| 28 |
-
# }
|
| 29 |
-
|
| 30 |
-
#def query(payload):
|
| 31 |
-
#generated_text = requests.post(API_URL, headers=headers, json=payload).json()
|
| 32 |
-
#print(generated_text)
|
| 33 |
-
#return generated_text["text"]
|
| 34 |
-
|
| 35 |
-
# Replace the following line with your text generation logic
|
| 36 |
-
#generated_text = f"Generated text based on the prompt: '{prompt}'"
|
| 37 |
-
|
| 38 |
-
# Initialize the text generation pipeline
|
| 39 |
-
text_generator = pipeline(model="lgaalves/gpt2-dolly", token=token)
|
| 40 |
-
|
| 41 |
-
# Generate text based on a prompt
|
| 42 |
-
generated_text = text_generator(prompt, max_length=500, num_return_sequences=1, temperature=0.7)
|
| 43 |
-
|
| 44 |
-
# Print the generated text
|
| 45 |
-
print(generated_text)
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
return generated_text
|
| 50 |
-
|
| 51 |
-
# Define the payload for the request
|
| 52 |
-
#payload = {
|
| 53 |
-
# "inputs": prompt # Adjust this based on your model's input format
|
| 54 |
-
#}
|
| 55 |
-
|
| 56 |
-
# Make the request to the API
|
| 57 |
-
#generated_text = requests.post(API_URL, headers=headers, json=payload).json()
|
| 58 |
-
|
| 59 |
-
# Extract and return the generated text
|
| 60 |
-
#return generated_text["generated_text"]
|
| 61 |
-
|
| 62 |
-
# Uncomment and customize the following lines based on your text generation needs
|
| 63 |
-
# text_generator = pipeline(model="gpt2")
|
| 64 |
-
# generated_text = text_generator(prompt, max_length=500, num_return_sequences=1, temperature=0.7)
|
| 65 |
-
|
| 66 |
-
# Print the generated text if needed
|
| 67 |
-
# print(generated_text)
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def tokenize(input_text):
|
| 5 |
+
tokens = tokenizer(input_text)["input_ids"]
|
| 6 |
+
return f"Number of tokens: {len(tokens)}"
|
| 7 |
|
| 8 |
+
tokenize_tool = gr.Interface(
|
| 9 |
+
fn=tokenize,
|
| 10 |
+
inputs=gr.Textbox(lines=7, label="Input Text"),
|
| 11 |
+
outputs=gr.Textbox(label="Tokenization Result"),
|
| 12 |
+
live=True,
|
| 13 |
+
title="GPT-2 Tokenizer",
|
| 14 |
+
description="This tool tokenizes input text using the lgaalves/gpt2-dolly model.",
|
| 15 |
+
)
|
| 16 |
|
| 17 |
+
tokenize_tool.launch()
|
| 18 |
+
|
| 19 |
+
import os
|
| 20 |
+
from transformers import pipeline
|
| 21 |
from transformers import Tool
|
|
|
|
| 22 |
|
| 23 |
+
class TokenCounterTool(Tool):
|
| 24 |
name = "text_generator"
|
| 25 |
+
description = "This is a tool for counting token used by a prompt. It takes a prompt as input and returns the generated text."
|
|
|
|
|
|
|
|
|
|
| 26 |
inputs = ["text"]
|
| 27 |
outputs = ["text"]
|
| 28 |
|
| 29 |
def __call__(self, prompt: str):
|
| 30 |
+
token = os.environ['hf']
|
| 31 |
+
tokenizer = AutoTokenizer.from_pretrained("lgaalves/gpt2-dolly")
|
| 32 |
+
tokens = tokenizer(input_text)["input_ids"]
|
| 33 |
+
return f"Number of tokens: {len(tokens)}"
|
| 34 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|