File size: 10,967 Bytes
d9b8e9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import gradio as gr
from database import create_db
from functions import *
from functions import _generate_code

# Supported models
models_options_general = ['GPT2', 'GPT2-medium', 'GPT2-large', 'GPT2-persian', 'GPT-Neo-125M']
models_options_codegen = ['codegen']
models_options_chatbot = ['dialoGPT', 'dialoGPT-medium', 'dialoGPT-large']

# Create database
create_db()

# Interface setup
with gr.Blocks() as interface:
    gr.Markdown(
        "# **GPT Tools**\n\n"
        "Generate something using GPT models. Select the model and adjust the parameters for optimal results."
    )
    with gr.Tabs():
        with gr.Tab("Text Generator"):
            with gr.Row():
                with gr.Column(scale=1, min_width=350):
                    input_text = gr.Textbox(label="Input Text", placeholder="Enter your text here...", lines=4, max_lines=6)
                    selected_model = gr.Radio(choices=models_options_general, value="GPT2", label="Select Model", type="value")
                    with gr.Row():
                        max_tokens = gr.Slider(10, 100, value=50, step=1, label="Max New Tokens", interactive=True)
                with gr.Column(scale=1, min_width=350):
                    output_text = gr.Textbox(label="Generated Text", interactive=False, lines=8, max_lines=12)
                    generate_button = gr.Button("Generate Text", variant="primary")

            generate_button.click(
                generate,
                inputs=[input_text, selected_model, max_tokens],
                outputs=output_text,
            )


        with gr.Tab("Multiverse Story Generator"):
            with gr.Row():
                with gr.Column(scale=1, min_width=350):
                    input_text = gr.Textbox(label="Enter your story idea", placeholder="e.g. A scientist discovers a parallel universe...", lines=4, max_lines=6)
                    selected_model = gr.Radio(choices=models_options_general, value="GPT2", label="Select Model for Story Generation", type="value")
                    max_length = gr.Slider(50, 300, value=150, step=1, label="Max Length", interactive=True)

                with gr.Column(scale=1, min_width=350):
                    output_text = gr.Textbox(label="Generated Worlds", interactive=False, lines=12, max_lines=20)
                    generate_button = gr.Button("Generate Parallel Worlds", variant="primary")

            generate_button.click(
                generate_multiverse,
                inputs=[input_text, selected_model, max_length],
                outputs=output_text,
            )

        with gr.Tab("Interactive Story Writing"):
            with gr.Row():
                with gr.Column(scale=1, min_width=350):
                    story_input = gr.Textbox(label="Add to Story", placeholder="Enter your part of the story...", lines=4, max_lines=6)
                    story_model = gr.Radio(choices=models_options_general, value="GPT2", label="Select Model", type="value")
                    story_max_length = gr.Slider(50, 300, value=50, step=1, label="Max Length", interactive=True)
                with gr.Column(scale=1, min_width=350):
                    story_text = gr.Textbox(label="Story So Far", interactive=False, lines=12, max_lines=20)
                    story_button = gr.Button("Generate Next Part", variant="primary")
                    reset_button = gr.Button("Reset Story", variant="secondary")

            story_button.click(
                interactive_story,
                inputs=[story_input, story_model, story_max_length],
                outputs=story_text,
            )
            reset_button.click(
                reset_story,
                inputs=[],
                outputs=story_text,
            )

        with gr.Tab("Training"):
            gr.Markdown("# **Train Model**\n\n")
            with gr.Column(scale=1, min_width=250):
                train_model_selector = gr.Radio(choices=models_options_general, value="GPT2", label="Select Model for Training", type="value")
                train_method = gr.Radio(
                    choices=["Custom Text", "Database", "Dataset File", "Hugging Face Dataset"],
                    value="Custom Text",
                    label="Training Method",
                    type="value"
                )
                dataset_name = gr.Textbox(label="Hugging Face Dataset Name", placeholder="Enter dataset name (e.g., ag_news)")
                split_name = gr.Textbox(label="Dataset Split", placeholder="e.g., train, test, validation")
                epochs = gr.Slider(1, 100, value=10, step=1, label="Epochs", interactive=True)
                batch_size = gr.Slider(1, 100, value=8, step=1, label="Batch Size", interactive=True)
                password = gr.Textbox(label="Enter Training Password", placeholder="Enter password", type="password")
                custom_text = gr.Textbox(label="Custom Text (optional)", placeholder="Enter custom text for training...")
                dataset_file = gr.File(label="Upload Dataset", type="filepath", file_types=[".parquet", ".csv", ".json", ".txt"])
                train_button = gr.Button("Train Model", variant="primary")
                train_status = gr.Textbox(label="Training Status", interactive=False)
            
            train_button.click(
                verify_and_train_combined,
                inputs=[train_model_selector, train_method, epochs, batch_size, password, custom_text, dataset_file, dataset_name, split_name],
                outputs=train_status,
            )
            train_button.click(
                verify_and_train_combined,
                inputs=[train_model_selector, train_method, epochs, batch_size, password, custom_text, dataset_file, dataset_name, split_name],
                outputs=train_status,
            )

        with gr.Tab("Code Generator"):
            gr.Markdown("### Generate Code from Descriptions")
            with gr.Row():
                with gr.Column(scale=1, min_width=350):
                    code_prompt = gr.Textbox(label="Code Prompt", placeholder="Describe your coding task, e.g., 'Write a Python function to calculate Fibonacci numbers.'")
                    code_max_tokens = gr.Slider(10, 500, value=150, step=10, label="Max Tokens")
                with gr.Column(scale=1, min_width=350):
                    generated_code = gr.Textbox(label="Generated Code", interactive=False, lines=10, max_lines=20)
                    generate_code_button = gr.Button("Generate Code")

            generate_code_button.click(
                _generate_code,
                inputs=[code_prompt, code_max_tokens],
                outputs=generated_code,
            )

        # Add AI-Powered Story World Builder Tab
        with gr.Tab("Story World Builder"):
            with gr.Row():
                with gr.Column(scale=1, min_width=350):
                    world_name = gr.Textbox(label="World Name", placeholder="Enter your world name...")
                    locations = gr.Textbox(label="Locations", placeholder="Enter locations separated by commas...")
                    characters = gr.Textbox(label="Characters", placeholder="Enter characters separated by commas...")
                    create_button = gr.Button("Create World", variant='primary')
                    generate_story_button = gr.Button("Generate Story")
                with gr.Column(scale=1, min_width=350):
                    world_status = gr.Textbox(label="World Status", interactive=False)
                    generated_story = gr.Textbox(label="Generated Story", interactive=False, lines=12, max_lines=20)


            create_button.click(
                define_world,
                inputs=[world_name, locations, characters],
                outputs=world_status,
            )
            
            gr.Markdown("### Generate a Story in Your World")
            with gr.Row():
                with gr.Column(scale=1, min_width=350):
                    story_world = gr.Textbox(label="Enter World Name", placeholder="World name...")
                    event = gr.Textbox(label="Event", placeholder="Describe an event in the world...")
                    selected_model = gr.Radio(choices=models_options_general, value="GPT2", label="Select Model", type="value")
                    max_length = gr.Slider(50, 300, value=150, step=1, label="Max Length")

        with gr.Tab("Chatbot"):
            gr.Markdown("### **Chat With AI Models**")
            with gr.Row():
                with gr.Column(scale=1, min_width=250):
                    username = gr.Textbox(label="Username", placeholder="Enter your username", lines=1)
                    chat_id = gr.Textbox(label="Chat ID (optional)", placeholder="Enter chat ID or leave blank for a new chat", lines=1)
                    selected_model = gr.Radio(models_options_chatbot, label="Select Model", value="dialoGPT")
                    send_button = gr.Button("Send", variant="primary")
                    reset_button = gr.Button("Reset Chat", variant="secondary")
                with gr.Column(scale=1, min_width=250):
                    input_text = gr.Textbox(label="Your Message", placeholder="Type your message here...", lines=2)
                    emotion_output = gr.Textbox(label="Detected Emotion", interactive=False)
                    chat_output = gr.Textbox(label="Chat History", lines=10, interactive=False)

            send_button.click(
                chatbot_response_with_emotion,
                inputs=[username, input_text, selected_model, chat_id],
                outputs=[chat_output, chat_id, emotion_output]
            )

            reset_button.click(
                reset_chat,
                inputs=[username],
                outputs=[chat_output]
            )
            gr.Markdown("---")
            gr.Markdown("### **Fetch Chat IDs**")
            with gr.Row():
                with gr.Column(scale=1, min_width=250):
                    username = gr.Textbox(label="Username", placeholder="Enter your username", lines=1)
                    fetch_btn = gr.Button("Fetch", variant="primary")
                with gr.Column(scale=1, min_width=250):
                    fetch_output = gr.Textbox(label="Chat IDs", lines=3, interactive=False)
            fetch_btn.click(
                chat_ids,
                inputs=[username],
                outputs=[fetch_output],
            )
    
    generate_story_button.click(
        generate_story,
        inputs=[selected_model, story_world, max_length, event],
        outputs=generated_story,
    )

    gr.Markdown("Made by **AliMc2021** with ❀️")

# Launch the interface
interface.queue().launch(
    server_port=7860, 
    show_error=True, 
    inline=False,
    #share=True,
)