File size: 4,048 Bytes
d5f516c
 
 
 
 
 
f005be3
d5f516c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f005be3
 
 
d5f516c
f005be3
 
 
 
d5f516c
f005be3
 
 
 
 
 
 
d5f516c
f005be3
 
 
 
 
 
 
 
 
 
 
 
 
d5f516c
 
f005be3
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
import os
import json
import vertexai
from vertexai.generative_models import GenerativeModel
import vertexai.preview.generative_models as generative_models
import gradio as gr
import pyperclip

# Read the service account key JSON file path from environment variable
SERVICE_ACCOUNT_KEY_PATH = os.getenv("GOOGLE_APPLICATION_CREDENTIALS")

if not SERVICE_ACCOUNT_KEY_PATH:
    raise ValueError("The GOOGLE_APPLICATION_CREDENTIALS environment variable is not set.")

with open(SERVICE_ACCOUNT_KEY_PATH) as f:
    service_account_info = json.load(f)
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = SERVICE_ACCOUNT_KEY_PATH

def generate(text):
    try:
        vertexai.init(project="idyllic-now-424815-h2", location="us-central1")
        model = GenerativeModel(
            "gemini-1.5-flash-001",
            system_instruction=[
                'Objective', text, 'Instructions', 'Use words like "thou," "thee," "thy," "henceforth," "forsooth," and "verily."',
                'Transform sentences to be elaborate and formal.',
                'Maintain a delusional nobleman tone, addressing others as if they are of lower status.',
                'Examples', ':', 'Input', ': "Hey, what\'s up?"', 'Output', ': "Greetings, fair compatriot! What news dost thou bring?"',
                'Input', ': "Can you help me with this?"', 'Output', ': "Might I entreat thee to lend thine esteemed assistance in this matter?"',
                'Input', ': "I don\'t like this."', 'Output', ': "I find this matter to be most displeasing and beneath my esteemed tastes."',
                'Input', ': "You did a good job."', 'Output', ': "Thy efforts are commendable, and thou hast performed admirably."',
                'Input', ': "See you later."', 'Output', ': "Until we meet again, may fortune smile upon thee."',
                'Keep the original meaning, but make it sound like a nobleman from a fantasy world.'
            ]
        )
        generation_config = {
            'max_output_tokens': 3019,
            'temperature': 1,
            'top_p': 0.32,
        }
        safety_settings = {
            generative_models.HarmCategory.HARM_CATEGORY_HATE_SPEECH: generative_models.HarmBlockThreshold.BLOCK_NONE,
            generative_models.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: generative_models.HarmBlockThreshold.BLOCK_NONE,
            generative_models.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: generative_models.HarmBlockThreshold.BLOCK_NONE,
            generative_models.HarmCategory.HARM_CATEGORY_HARASSMENT: generative_models.HarmBlockThreshold.BLOCK_NONE,
        }
        responses = model.generate_content(
            [text],
            generation_config=generation_config,
            safety_settings=safety_settings,
            stream=True,
        )

        response_text = ""
        for response in responses:
            response_text += response.text

        return response_text if response_text else "No valid response generated or response was blocked."

    except Exception as e:
        return str(e)

def copy_to_clipboard(text):
    pyperclip.copy(text)
    return "Text copied to clipboard!"

def update_output_and_copy(text):
    response_text = generate(text)
    pyperclip.copy(response_text)
    return gr.update(value=response_text, visible=True), "Text copied to clipboard!"

iface = gr.Interface(
    fn=generate,
    inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
    outputs=gr.Textbox(visible=True),
    title="Chuunibyou Text Generator",
    description="Transform text into an elaborate and formal style with a Chuunibyou tone."
)

iface_with_button = gr.Blocks()

with iface_with_button:
    textbox = gr.Textbox(lines=2, placeholder="Enter text here...")
    output = gr.Textbox(visible=True)
    submit_button = gr.Button("Submit")
    copy_message = gr.Textbox(visible=True)

    submit_button.click(
        fn=update_output_and_copy,
        inputs=textbox,
        outputs=[output, copy_message]
    )

if __name__ == "__main__":
    iface_with_button.launch()