Spaces:
Running
Running
Update text_generator.py
Browse files- text_generator.py +93 -68
text_generator.py
CHANGED
|
@@ -1,68 +1,93 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
#
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
def generate_files(title="Text Generation Tool", emoji="🌖", colorFrom="blue", colorTo="blue",
|
| 5 |
+
sdk="gradio", sdk_version="4.3.0", app_file="app.py", pinned=False,
|
| 6 |
+
tags=["tool"], tool_name="text_generator", tool_description="This is a tool that chats with a user. "
|
| 7 |
+
"It takes an input named `prompt` which contains a system_role, user_message, context and history. "
|
| 8 |
+
"It returns a text message."):
|
| 9 |
+
# Generate readme content
|
| 10 |
+
readme_content = f'''## readme
|
| 11 |
+
title: {title}
|
| 12 |
+
emoji: {emoji}
|
| 13 |
+
colorFrom: {colorFrom}
|
| 14 |
+
colorTo: {colorTo}
|
| 15 |
+
sdk: {sdk}
|
| 16 |
+
sdk_version: {sdk_version}
|
| 17 |
+
app_file: {app_file}
|
| 18 |
+
pinned: {pinned}
|
| 19 |
+
tags:
|
| 20 |
+
- {tags[0]}
|
| 21 |
+
'''
|
| 22 |
+
|
| 23 |
+
# Generate tool config JSON content
|
| 24 |
+
tool_config = {
|
| 25 |
+
"description": tool_description,
|
| 26 |
+
"name": tool_name,
|
| 27 |
+
"tool_class": f"{tool_name.capitalize()}Tool"
|
| 28 |
+
}
|
| 29 |
+
tool_config_json = json.dumps(tool_config, indent=4)
|
| 30 |
+
|
| 31 |
+
# Generate app.py content
|
| 32 |
+
app_py_content = f'''from transformers.tools.base import launch_gradio_demo
|
| 33 |
+
from {tool_name} import {tool_name.capitalize()}Tool
|
| 34 |
+
|
| 35 |
+
launch_gradio_demo({tool_name.capitalize()}Tool)
|
| 36 |
+
'''
|
| 37 |
+
|
| 38 |
+
# Generate requirements.txt content
|
| 39 |
+
requirements_content = '''transformers>=4.29.0
|
| 40 |
+
# diffusers
|
| 41 |
+
accelerate
|
| 42 |
+
torch
|
| 43 |
+
'''
|
| 44 |
+
|
| 45 |
+
# Generate text_generator.py content
|
| 46 |
+
text_generator_py_content = f'''import os
|
| 47 |
+
from transformers import pipeline
|
| 48 |
+
from transformers import Tool
|
| 49 |
+
|
| 50 |
+
class {tool_name.capitalize()}Tool(Tool):
|
| 51 |
+
name = "{tool_name}"
|
| 52 |
+
description = (
|
| 53 |
+
"{tool_description}"
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
inputs = ["text"]
|
| 57 |
+
outputs = ["text"]
|
| 58 |
+
|
| 59 |
+
def __call__(self, prompt: str):
|
| 60 |
+
token = os.environ['hf']
|
| 61 |
+
text_generator = pipeline(model="microsoft/Orca-2-13b", token=token)
|
| 62 |
+
generated_text = text_generator(prompt, max_length=500, num_return_sequences=1, temperature=0.7)
|
| 63 |
+
print(generated_text)
|
| 64 |
+
return generated_text
|
| 65 |
+
'''
|
| 66 |
+
|
| 67 |
+
# Write content to files
|
| 68 |
+
with open("README.md", "w") as readme_file:
|
| 69 |
+
readme_file.write(readme_content)
|
| 70 |
+
|
| 71 |
+
with open("tool_config.json", "w") as tool_config_file:
|
| 72 |
+
tool_config_file.write(tool_config_json)
|
| 73 |
+
|
| 74 |
+
with open("app.py", "w") as app_py_file:
|
| 75 |
+
app_py_file.write(app_py_content)
|
| 76 |
+
|
| 77 |
+
with open("requirements.txt", "w") as requirements_file:
|
| 78 |
+
requirements_file.write(requirements_content)
|
| 79 |
+
|
| 80 |
+
with open(f"{tool_name}.py", "w") as text_generator_py_file:
|
| 81 |
+
text_generator_py_file.write(text_generator_py_content)
|
| 82 |
+
|
| 83 |
+
# Return the generated files for download
|
| 84 |
+
return "README.md", "tool_config.json", "app.py", "requirements.txt", f"{tool_name}.py"
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
# Define the inputs for the Gradio interface
|
| 88 |
+
io = gr.Interface(generate_files,
|
| 89 |
+
inputs=["text", "text", "text", "text", "text", "text", "text", "text", "checkbox", "text", "text"],
|
| 90 |
+
outputs=["text", "text", "text", "text", "text"])
|
| 91 |
+
|
| 92 |
+
# Launch the Gradio interface
|
| 93 |
+
io.launch()
|