Chris4K commited on
Commit
ffbd4f4
·
verified ·
1 Parent(s): 2a0f189

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -15
app.py CHANGED
@@ -25,11 +25,15 @@ def generate_files(title="Text Generation Tool", tool_description="This is a too
25
  ---
26
  '''.format(title)
27
 
 
 
 
 
28
  # Generate tool config JSON content
29
  tool_config = {
30
  "description": tool_description,
31
- "name": title.replace(" ","_").lower(),
32
- "tool_class": "{}Tool".format(title.replace(" ",""))
33
  }
34
  tool_config_json = json.dumps(tool_config, indent=4)
35
 
@@ -39,7 +43,7 @@ def generate_files(title="Text Generation Tool", tool_description="This is a too
39
  import launch_gradio_demo
40
  from {} import {}
41
  launch_gradio_demo({}Tool)
42
- '''.format( title.replace(" ","_").lower(), title.replace(" ",""), title.replace(" ",""))
43
 
44
  # Generate requirements.txt content
45
  requirements_content = '''transformers>=4.29.0
@@ -64,45 +68,45 @@ class {}(Tool):
64
  generated_text = text_generator(prompt, max_length=500, num_return_sequences=1, temperature=0.7)
65
  print(generated_text)
66
  return generated_text
67
- '''.format(title.replace(" ",""), title.replace(" ","_").lower(), tool_description)
68
 
69
  # Create a new folder for the tool
70
- os.makedirs(title.replace(" ",""), exist_ok=True)
71
 
72
  # Write content to files
73
- with open(f"{title.replace(" ","")}/README.md", "w") as readme_file:
74
  readme_file.write(readme_content)
75
 
76
- with open(f"{title.replace(" ","")}/tool_config.json", "w") as tool_config_file:
77
  tool_config_file.write(tool_config_json)
78
 
79
- with open(f"{title.replace(" ","")}/app.py", "w") as app_py_file:
80
  app_py_file.write(app_py_content)
81
 
82
- with open(f"{title.replace(" ","")}/requirements.txt", "w") as requirements_file:
83
  requirements_file.write(requirements_content)
84
 
85
- with open(f"{title.replace(" ","")}/app.py", "w") as text_generator_py_file:
86
  text_generator_py_file.write(text_generator_py_content)
87
 
88
- create_repo(repo_id=title.replace(" ","-"), repo_type="space", space_sdk = "gradio")
89
  #repo_type="space"
90
 
91
  # Upload the folder to the Hugging Face Hub
92
  upload_folder(
93
- folder_path=title.replace(" ",""),
94
- repo_id=f"Chris4K/{title.replace(" ","")}",
95
  repo_type="space"
96
  )
97
 
98
  # Return the generated files for download
99
- return "README.md", "tool_config.json", "app.py", "requirements.txt", f"{title.replace(" ","_").lower()}.py"
100
 
101
 
102
  # Define the inputs for the Gradio interface
103
  io = gr.Interface(generate_files,
104
  inputs=[gr.inputs.Text(default="Text Generation Tool"), gr.inputs.Text(default="It takes an input named `prompt` which contains a system_role, user_message, context and history. It returns a text message.")],
105
- outputs=["text", "text", "text", "text", "text"])
106
 
107
  # Launch the Gradio interface
108
  io.launch()
 
25
  ---
26
  '''.format(title)
27
 
28
+ tool_name = title.replace(" ","_").lower()
29
+ tool_class = title.replace(" ","")
30
+ tool_repo_id = title.replace(" ","-")
31
+
32
  # Generate tool config JSON content
33
  tool_config = {
34
  "description": tool_description,
35
+ "name": tool_name,
36
+ "tool_class": "{}Tool".format(tool_class)
37
  }
38
  tool_config_json = json.dumps(tool_config, indent=4)
39
 
 
43
  import launch_gradio_demo
44
  from {} import {}
45
  launch_gradio_demo({}Tool)
46
+ '''.format( tool_name, tool_class, tool_class)
47
 
48
  # Generate requirements.txt content
49
  requirements_content = '''transformers>=4.29.0
 
68
  generated_text = text_generator(prompt, max_length=500, num_return_sequences=1, temperature=0.7)
69
  print(generated_text)
70
  return generated_text
71
+ '''.format(tool_class, tool_name, tool_description)
72
 
73
  # Create a new folder for the tool
74
+ os.makedirs(tool_class, exist_ok=True)
75
 
76
  # Write content to files
77
+ with open(f"{tool_class}/README.md", "w") as readme_file:
78
  readme_file.write(readme_content)
79
 
80
+ with open(f"{tool_class}/tool_config.json", "w") as tool_config_file:
81
  tool_config_file.write(tool_config_json)
82
 
83
+ with open(f"{tool_class}/app.py", "w") as app_py_file:
84
  app_py_file.write(app_py_content)
85
 
86
+ with open(f"{tool_class}/requirements.txt", "w") as requirements_file:
87
  requirements_file.write(requirements_content)
88
 
89
+ with open(f"{tool_class}/app.py", "w") as text_generator_py_file:
90
  text_generator_py_file.write(text_generator_py_content)
91
 
92
+ create_repo(repo_id=tool_repo_id, repo_type="space", space_sdk = "gradio")
93
  #repo_type="space"
94
 
95
  # Upload the folder to the Hugging Face Hub
96
  upload_folder(
97
+ folder_path=tool_class,
98
+ repo_id=f"Chris4K/{tool_class}",
99
  repo_type="space"
100
  )
101
 
102
  # Return the generated files for download
103
+ return f"Chris4K/{tool_class}"
104
 
105
 
106
  # Define the inputs for the Gradio interface
107
  io = gr.Interface(generate_files,
108
  inputs=[gr.inputs.Text(default="Text Generation Tool"), gr.inputs.Text(default="It takes an input named `prompt` which contains a system_role, user_message, context and history. It returns a text message.")],
109
+ outputs=["text"])
110
 
111
  # Launch the Gradio interface
112
  io.launch()