AliMc2021 commited on
Commit
2c56ec6
Β·
verified Β·
1 Parent(s): 8186752

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +205 -205
app.py CHANGED
@@ -1,206 +1,206 @@
1
- import gradio as gr
2
- from database import create_db
3
- from functions import *
4
- from functions import _generate_code
5
-
6
- # Supported models
7
- models_options_general = ['GPT2', 'GPT2-medium', 'GPT2-large', 'GPT2-persian', 'GPT-Neo-125M']
8
- models_options_codegen = ['codegen']
9
- models_options_chatbot = ['dialoGPT', 'dialoGPT-medium', 'dialoGPT-large']
10
-
11
- # Create database
12
- create_db()
13
-
14
- # Interface setup
15
- with gr.Blocks() as interface:
16
- gr.Markdown(
17
- "# **GPT Tools**\n\n"
18
- "Generate something using GPT models. Select the model and adjust the parameters for optimal results."
19
- )
20
- with gr.Tabs():
21
- with gr.Tab("Text Generator"):
22
- with gr.Row():
23
- with gr.Column(scale=1, min_width=350):
24
- input_text = gr.Textbox(label="Input Text", placeholder="Enter your text here...", lines=4, max_lines=6)
25
- selected_model = gr.Radio(choices=models_options_general, value="GPT2", label="Select Model", type="value")
26
- with gr.Row():
27
- max_tokens = gr.Slider(10, 100, value=50, step=1, label="Max New Tokens", interactive=True)
28
- with gr.Column(scale=1, min_width=350):
29
- output_text = gr.Textbox(label="Generated Text", interactive=False, lines=8, max_lines=12)
30
- generate_button = gr.Button("Generate Text", variant="primary")
31
-
32
- generate_button.click(
33
- generate,
34
- inputs=[input_text, selected_model, max_tokens],
35
- outputs=output_text,
36
- )
37
-
38
-
39
- with gr.Tab("Multiverse Story Generator"):
40
- with gr.Row():
41
- with gr.Column(scale=1, min_width=350):
42
- input_text = gr.Textbox(label="Enter your story idea", placeholder="e.g. A scientist discovers a parallel universe...", lines=4, max_lines=6)
43
- selected_model = gr.Radio(choices=models_options_general, value="GPT2", label="Select Model for Story Generation", type="value")
44
- max_length = gr.Slider(50, 300, value=150, step=1, label="Max Length", interactive=True)
45
-
46
- with gr.Column(scale=1, min_width=350):
47
- output_text = gr.Textbox(label="Generated Worlds", interactive=False, lines=12, max_lines=20)
48
- generate_button = gr.Button("Generate Parallel Worlds", variant="primary")
49
-
50
- generate_button.click(
51
- generate_multiverse,
52
- inputs=[input_text, selected_model, max_length],
53
- outputs=output_text,
54
- )
55
-
56
- with gr.Tab("Interactive Story Writing"):
57
- with gr.Row():
58
- with gr.Column(scale=1, min_width=350):
59
- story_input = gr.Textbox(label="Add to Story", placeholder="Enter your part of the story...", lines=4, max_lines=6)
60
- story_model = gr.Radio(choices=models_options_general, value="GPT2", label="Select Model", type="value")
61
- story_max_length = gr.Slider(50, 300, value=50, step=1, label="Max Length", interactive=True)
62
- with gr.Column(scale=1, min_width=350):
63
- story_text = gr.Textbox(label="Story So Far", interactive=False, lines=12, max_lines=20)
64
- story_button = gr.Button("Generate Next Part", variant="primary")
65
- reset_button = gr.Button("Reset Story", variant="secondary")
66
-
67
- story_button.click(
68
- interactive_story,
69
- inputs=[story_input, story_model, story_max_length],
70
- outputs=story_text,
71
- )
72
- reset_button.click(
73
- reset_story,
74
- inputs=[],
75
- outputs=story_text,
76
- )
77
-
78
- with gr.Tab("Training"):
79
- gr.Markdown("# **Train Model**\n\n")
80
- with gr.Column(scale=1, min_width=250):
81
- train_model_selector = gr.Radio(choices=models_options_general, value="GPT2", label="Select Model for Training", type="value")
82
- train_method = gr.Radio(
83
- choices=["Custom Text", "Database", "Dataset File", "Hugging Face Dataset"],
84
- value="Custom Text",
85
- label="Training Method",
86
- type="value"
87
- )
88
- dataset_name = gr.Textbox(label="Hugging Face Dataset Name", placeholder="Enter dataset name (e.g., ag_news)")
89
- split_name = gr.Textbox(label="Dataset Split", placeholder="e.g., train, test, validation")
90
- epochs = gr.Slider(1, 100, value=10, step=1, label="Epochs", interactive=True)
91
- batch_size = gr.Slider(1, 100, value=8, step=1, label="Batch Size", interactive=True)
92
- password = gr.Textbox(label="Enter Training Password", placeholder="Enter password", type="password")
93
- custom_text = gr.Textbox(label="Custom Text (optional)", placeholder="Enter custom text for training...")
94
- dataset_file = gr.File(label="Upload Dataset", type="filepath", file_types=[".parquet", ".csv", ".json", ".txt"])
95
- train_button = gr.Button("Train Model", variant="primary")
96
- train_status = gr.Textbox(label="Training Status", interactive=False)
97
-
98
- train_button.click(
99
- verify_and_train_combined,
100
- inputs=[train_model_selector, train_method, epochs, batch_size, password, custom_text, dataset_file, dataset_name, split_name],
101
- outputs=train_status,
102
- )
103
- train_button.click(
104
- verify_and_train_combined,
105
- inputs=[train_model_selector, train_method, epochs, batch_size, password, custom_text, dataset_file, dataset_name, split_name],
106
- outputs=train_status,
107
- )
108
-
109
- with gr.Tab("Code Generator"):
110
- gr.Markdown("### Generate Code from Descriptions")
111
- with gr.Row():
112
- with gr.Column(scale=1, min_width=350):
113
- code_prompt = gr.Textbox(label="Code Prompt", placeholder="Describe your coding task, e.g., 'Write a Python function to calculate Fibonacci numbers.'")
114
- code_max_tokens = gr.Slider(10, 500, value=150, step=10, label="Max Tokens")
115
- with gr.Column(scale=1, min_width=350):
116
- generated_code = gr.Textbox(label="Generated Code", interactive=False, lines=10, max_lines=20)
117
- generate_code_button = gr.Button("Generate Code")
118
-
119
- generate_code_button.click(
120
- _generate_code,
121
- inputs=[code_prompt, code_max_tokens],
122
- outputs=generated_code,
123
- )
124
-
125
- # Add AI-Powered Story World Builder Tab
126
- with gr.Tab("Story World Builder"):
127
- with gr.Row():
128
- with gr.Column(scale=1, min_width=350):
129
- world_name = gr.Textbox(label="World Name", placeholder="Enter your world name...")
130
- locations = gr.Textbox(label="Locations", placeholder="Enter locations separated by commas...")
131
- characters = gr.Textbox(label="Characters", placeholder="Enter characters separated by commas...")
132
- create_button = gr.Button("Create World", variant='primary')
133
- generate_story_button = gr.Button("Generate Story")
134
- with gr.Column(scale=1, min_width=350):
135
- world_status = gr.Textbox(label="World Status", interactive=False)
136
- generated_story = gr.Textbox(label="Generated Story", interactive=False, lines=12, max_lines=20)
137
-
138
-
139
- create_button.click(
140
- define_world,
141
- inputs=[world_name, locations, characters],
142
- outputs=world_status,
143
- )
144
-
145
- gr.Markdown("### Generate a Story in Your World")
146
- with gr.Row():
147
- with gr.Column(scale=1, min_width=350):
148
- story_world = gr.Textbox(label="Enter World Name", placeholder="World name...")
149
- event = gr.Textbox(label="Event", placeholder="Describe an event in the world...")
150
- selected_model = gr.Radio(choices=models_options_general, value="GPT2", label="Select Model", type="value")
151
- max_length = gr.Slider(50, 300, value=150, step=1, label="Max Length")
152
-
153
- with gr.Tab("Chatbot"):
154
- gr.Markdown("### **Chat With AI Models**")
155
- with gr.Row():
156
- with gr.Column(scale=1, min_width=250):
157
- username = gr.Textbox(label="Username", placeholder="Enter your username", lines=1)
158
- chat_id = gr.Textbox(label="Chat ID (optional)", placeholder="Enter chat ID or leave blank for a new chat", lines=1)
159
- selected_model = gr.Radio(models_options_chatbot, label="Select Model", value="dialoGPT")
160
- send_button = gr.Button("Send", variant="primary")
161
- reset_button = gr.Button("Reset Chat", variant="secondary")
162
- with gr.Column(scale=1, min_width=250):
163
- input_text = gr.Textbox(label="Your Message", placeholder="Type your message here...", lines=2)
164
- emotion_output = gr.Textbox(label="Detected Emotion", interactive=False)
165
- chat_output = gr.Textbox(label="Chat History", lines=10, interactive=False)
166
-
167
- send_button.click(
168
- chatbot_response_with_emotion,
169
- inputs=[username, input_text, selected_model, chat_id],
170
- outputs=[chat_output, chat_id, emotion_output]
171
- )
172
-
173
- reset_button.click(
174
- reset_chat,
175
- inputs=[username],
176
- outputs=[chat_output]
177
- )
178
- gr.Markdown("---")
179
- gr.Markdown("### **Fetch Chat IDs**")
180
- with gr.Row():
181
- with gr.Column(scale=1, min_width=250):
182
- username = gr.Textbox(label="Username", placeholder="Enter your username", lines=1)
183
- fetch_btn = gr.Button("Fetch", variant="primary")
184
- with gr.Column(scale=1, min_width=250):
185
- fetch_output = gr.Textbox(label="Chat IDs", lines=3, interactive=False)
186
- fetch_btn.click(
187
- chat_ids,
188
- inputs=[username],
189
- outputs=[fetch_output],
190
- )
191
-
192
- generate_story_button.click(
193
- generate_story,
194
- inputs=[selected_model, story_world, max_length, event],
195
- outputs=generated_story,
196
- )
197
-
198
- gr.Markdown("Made by **AliMc2021** with ❀️")
199
-
200
- # Launch the interface
201
- interface.queue().launch(
202
- server_port=7860,
203
- show_error=True,
204
- inline=False,
205
- #share=True,
206
  )
 
1
+ import gradio as gr
2
+ from database import create_db
3
+ from functions import *
4
+ from functions import _generate_code
5
+
6
+ # Supported models
7
+ models_options_general = ['GPT2', 'GPT2-medium', 'GPT2-large', 'GPT2-persian', 'GPT-Neo-125M']
8
+ models_options_codegen = ['codegen']
9
+ models_options_chatbot = ['dialoGPT', 'dialoGPT-medium', 'dialoGPT-large']
10
+
11
+ # Create database
12
+ create_db()
13
+
14
+ # Interface setup
15
+ with gr.Blocks() as interface:
16
+ gr.Markdown(
17
+ "# **GPT Tools**\n\n"
18
+ "Generate something using GPT models. Select the model and adjust the parameters for optimal results."
19
+ )
20
+ with gr.Tabs():
21
+ with gr.Tab("Text Generator"):
22
+ with gr.Row():
23
+ with gr.Column(scale=1, min_width=350):
24
+ input_text = gr.Textbox(label="Input Text", placeholder="Enter your text here...", lines=4, max_lines=6)
25
+ selected_model = gr.Radio(choices=models_options_general, value="GPT2", label="Select Model", type="value")
26
+ with gr.Row():
27
+ max_tokens = gr.Slider(10, 100, value=50, step=1, label="Max New Tokens", interactive=True)
28
+ with gr.Column(scale=1, min_width=350):
29
+ output_text = gr.Textbox(label="Generated Text", interactive=False, lines=8, max_lines=12)
30
+ generate_button = gr.Button("Generate Text", variant="primary")
31
+
32
+ generate_button.click(
33
+ generate,
34
+ inputs=[input_text, selected_model, max_tokens],
35
+ outputs=output_text,
36
+ )
37
+
38
+
39
+ with gr.Tab("Multiverse Story Generator"):
40
+ with gr.Row():
41
+ with gr.Column(scale=1, min_width=350):
42
+ input_text = gr.Textbox(label="Enter your story idea", placeholder="e.g. A scientist discovers a parallel universe...", lines=4, max_lines=6)
43
+ selected_model = gr.Radio(choices=models_options_general, value="GPT2", label="Select Model for Story Generation", type="value")
44
+ max_length = gr.Slider(50, 300, value=150, step=1, label="Max Length", interactive=True)
45
+
46
+ with gr.Column(scale=1, min_width=350):
47
+ output_text = gr.Textbox(label="Generated Worlds", interactive=False, lines=12, max_lines=20)
48
+ generate_button = gr.Button("Generate Parallel Worlds", variant="primary")
49
+
50
+ generate_button.click(
51
+ generate_multiverse,
52
+ inputs=[input_text, selected_model, max_length],
53
+ outputs=output_text,
54
+ )
55
+
56
+ with gr.Tab("Interactive Story Writing"):
57
+ with gr.Row():
58
+ with gr.Column(scale=1, min_width=350):
59
+ story_input = gr.Textbox(label="Add to Story", placeholder="Enter your part of the story...", lines=4, max_lines=6)
60
+ story_model = gr.Radio(choices=models_options_general, value="GPT2", label="Select Model", type="value")
61
+ story_max_length = gr.Slider(50, 300, value=50, step=1, label="Max Length", interactive=True)
62
+ with gr.Column(scale=1, min_width=350):
63
+ story_text = gr.Textbox(label="Story So Far", interactive=False, lines=12, max_lines=20)
64
+ story_button = gr.Button("Generate Next Part", variant="primary")
65
+ reset_button = gr.Button("Reset Story", variant="secondary")
66
+
67
+ story_button.click(
68
+ interactive_story,
69
+ inputs=[story_input, story_model, story_max_length],
70
+ outputs=story_text,
71
+ )
72
+ reset_button.click(
73
+ reset_story,
74
+ inputs=[],
75
+ outputs=story_text,
76
+ )
77
+
78
+ with gr.Tab("Training"):
79
+ gr.Markdown("# **Train Model**\n\n")
80
+ with gr.Column(scale=1, min_width=250):
81
+ train_model_selector = gr.Radio(choices=models_options_general, value="GPT2", label="Select Model for Training", type="value")
82
+ train_method = gr.Radio(
83
+ choices=["Custom Text", "Database", "Dataset File", "Hugging Face Dataset"],
84
+ value="Custom Text",
85
+ label="Training Method",
86
+ type="value"
87
+ )
88
+ dataset_name = gr.Textbox(label="Hugging Face Dataset Name", placeholder="Enter dataset name (e.g., ag_news)")
89
+ split_name = gr.Textbox(label="Dataset Split", placeholder="e.g., train, test, validation")
90
+ epochs = gr.Slider(1, 100, value=10, step=1, label="Epochs", interactive=True)
91
+ batch_size = gr.Slider(1, 100, value=8, step=1, label="Batch Size", interactive=True)
92
+ password = gr.Textbox(label="Enter Training Password", placeholder="Enter password", type="password")
93
+ custom_text = gr.Textbox(label="Custom Text (optional)", placeholder="Enter custom text for training...")
94
+ dataset_file = gr.File(label="Upload Dataset", type="filepath", file_types=[".parquet", ".csv", ".json", ".txt"])
95
+ train_button = gr.Button("Train Model", variant="primary")
96
+ train_status = gr.Textbox(label="Training Status", interactive=False)
97
+
98
+ train_button.click(
99
+ verify_and_train_combined,
100
+ inputs=[train_model_selector, train_method, epochs, batch_size, password, custom_text, dataset_file, dataset_name, split_name],
101
+ outputs=train_status,
102
+ )
103
+ train_button.click(
104
+ verify_and_train_combined,
105
+ inputs=[train_model_selector, train_method, epochs, batch_size, password, custom_text, dataset_file, dataset_name, split_name],
106
+ outputs=train_status,
107
+ )
108
+
109
+ with gr.Tab("Code Generator"):
110
+ gr.Markdown("### Generate Code from Descriptions")
111
+ with gr.Row():
112
+ with gr.Column(scale=1, min_width=350):
113
+ code_prompt = gr.Textbox(label="Code Prompt", placeholder="Describe your coding task, e.g., 'Write a Python function to calculate Fibonacci numbers.'")
114
+ code_max_tokens = gr.Slider(10, 500, value=150, step=10, label="Max Tokens")
115
+ with gr.Column(scale=1, min_width=350):
116
+ generated_code = gr.Textbox(label="Generated Code", interactive=False, lines=10, max_lines=20)
117
+ generate_code_button = gr.Button("Generate Code", variant="primary")
118
+
119
+ generate_code_button.click(
120
+ _generate_code,
121
+ inputs=[code_prompt, code_max_tokens],
122
+ outputs=generated_code,
123
+ )
124
+
125
+ # Add AI-Powered Story World Builder Tab
126
+ with gr.Tab("Story World Builder"):
127
+ with gr.Row():
128
+ with gr.Column(scale=1, min_width=350):
129
+ world_name = gr.Textbox(label="World Name", placeholder="Enter your world name...")
130
+ locations = gr.Textbox(label="Locations", placeholder="Enter locations separated by commas...")
131
+ characters = gr.Textbox(label="Characters", placeholder="Enter characters separated by commas...")
132
+ create_button = gr.Button("Create World", variant='primary')
133
+ generate_story_button = gr.Button("Generate Story")
134
+ with gr.Column(scale=1, min_width=350):
135
+ world_status = gr.Textbox(label="World Status", interactive=False)
136
+ generated_story = gr.Textbox(label="Generated Story", interactive=False, lines=12, max_lines=20)
137
+
138
+
139
+ create_button.click(
140
+ define_world,
141
+ inputs=[world_name, locations, characters],
142
+ outputs=world_status,
143
+ )
144
+
145
+ gr.Markdown("### Generate a Story in Your World")
146
+ with gr.Row():
147
+ with gr.Column(scale=1, min_width=350):
148
+ story_world = gr.Textbox(label="Enter World Name", placeholder="World name...")
149
+ event = gr.Textbox(label="Event", placeholder="Describe an event in the world...")
150
+ selected_model = gr.Radio(choices=models_options_general, value="GPT2", label="Select Model", type="value")
151
+ max_length = gr.Slider(50, 300, value=150, step=1, label="Max Length")
152
+
153
+ with gr.Tab("Chatbot"):
154
+ gr.Markdown("### **Chat With AI Models**")
155
+ with gr.Row():
156
+ with gr.Column(scale=1, min_width=250):
157
+ username = gr.Textbox(label="Username", placeholder="Enter your username", lines=1)
158
+ chat_id = gr.Textbox(label="Chat ID (optional)", placeholder="Enter chat ID or leave blank for a new chat", lines=1)
159
+ selected_model = gr.Radio(models_options_chatbot, label="Select Model", value="dialoGPT")
160
+ send_button = gr.Button("Send", variant="primary")
161
+ reset_button = gr.Button("Reset Chat", variant="secondary")
162
+ with gr.Column(scale=1, min_width=250):
163
+ input_text = gr.Textbox(label="Your Message", placeholder="Type your message here...", lines=2)
164
+ emotion_output = gr.Textbox(label="Detected Emotion", interactive=False)
165
+ chat_output = gr.Textbox(label="Chat History", lines=10, interactive=False)
166
+
167
+ send_button.click(
168
+ chatbot_response_with_emotion,
169
+ inputs=[username, input_text, selected_model, chat_id],
170
+ outputs=[chat_output, chat_id, emotion_output]
171
+ )
172
+
173
+ reset_button.click(
174
+ reset_chat,
175
+ inputs=[username],
176
+ outputs=[chat_output]
177
+ )
178
+ gr.Markdown("---")
179
+ gr.Markdown("### **Fetch Chat IDs**")
180
+ with gr.Row():
181
+ with gr.Column(scale=1, min_width=250):
182
+ username = gr.Textbox(label="Username", placeholder="Enter your username", lines=1)
183
+ fetch_btn = gr.Button("Fetch", variant="primary")
184
+ with gr.Column(scale=1, min_width=250):
185
+ fetch_output = gr.Textbox(label="Chat IDs", lines=3, interactive=False)
186
+ fetch_btn.click(
187
+ chat_ids,
188
+ inputs=[username],
189
+ outputs=[fetch_output],
190
+ )
191
+
192
+ generate_story_button.click(
193
+ generate_story,
194
+ inputs=[selected_model, story_world, max_length, event],
195
+ outputs=generated_story,
196
+ )
197
+
198
+ gr.Markdown("Made by **AliMc2021** with ❀️")
199
+
200
+ # Launch the interface
201
+ interface.queue().launch(
202
+ server_port=7860,
203
+ show_error=True,
204
+ inline=False,
205
+ #share=True,
206
  )