prabinpanta0 commited on
Commit
d5f516c
·
verified ·
1 Parent(s): 0bfd4cd

Create app2.py

Browse files
Files changed (1) hide show
  1. app2.py +92 -0
app2.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import vertexai
4
+ from vertexai.generative_models import GenerativeModel
5
+ import vertexai.preview.generative_models as generative_models
6
+ import gradio as gr
7
+
8
+ # Read the service account key JSON file path from environment variable
9
+ SERVICE_ACCOUNT_KEY_PATH = os.getenv("GOOGLE_APPLICATION_CREDENTIALS")
10
+
11
+ if not SERVICE_ACCOUNT_KEY_PATH:
12
+ raise ValueError("The GOOGLE_APPLICATION_CREDENTIALS environment variable is not set.")
13
+
14
+ with open(SERVICE_ACCOUNT_KEY_PATH) as f:
15
+ service_account_info = json.load(f)
16
+ os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = SERVICE_ACCOUNT_KEY_PATH
17
+
18
+ def generate(text):
19
+ try:
20
+ vertexai.init(project="idyllic-now-424815-h2", location="us-central1")
21
+ model = GenerativeModel(
22
+ "gemini-1.5-flash-001",
23
+ system_instruction=[
24
+ 'Objective', text, 'Instructions', 'Use words like "thou," "thee," "thy," "henceforth," "forsooth," and "verily."',
25
+ 'Transform sentences to be elaborate and formal.',
26
+ 'Maintain a delusional nobleman tone, addressing others as if they are of lower status.',
27
+ 'Examples', ':', 'Input', ': "Hey, what\'s up?"', 'Output', ': "Greetings, fair compatriot! What news dost thou bring?"',
28
+ 'Input', ': "Can you help me with this?"', 'Output', ': "Might I entreat thee to lend thine esteemed assistance in this matter?"',
29
+ 'Input', ': "I don\'t like this."', 'Output', ': "I find this matter to be most displeasing and beneath my esteemed tastes."',
30
+ 'Input', ': "You did a good job."', 'Output', ': "Thy efforts are commendable, and thou hast performed admirably."',
31
+ 'Input', ': "See you later."', 'Output', ': "Until we meet again, may fortune smile upon thee."',
32
+ 'Keep the original meaning, but make it sound like a nobleman from a fantasy world.'
33
+ ]
34
+ )
35
+ generation_config = {
36
+ 'max_output_tokens': 3019,
37
+ 'temperature': 1,
38
+ 'top_p': 0.32,
39
+ }
40
+ safety_settings = {
41
+ generative_models.HarmCategory.HARM_CATEGORY_HATE_SPEECH: generative_models.HarmBlockThreshold.BLOCK_NONE,
42
+ generative_models.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: generative_models.HarmBlockThreshold.BLOCK_NONE,
43
+ generative_models.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: generative_models.HarmBlockThreshold.BLOCK_NONE,
44
+ generative_models.HarmCategory.HARM_CATEGORY_HARASSMENT: generative_models.HarmBlockThreshold.BLOCK_NONE,
45
+ }
46
+ responses = model.generate_content(
47
+ [text],
48
+ generation_config=generation_config,
49
+ safety_settings=safety_settings,
50
+ stream=True,
51
+ )
52
+
53
+ response_text = ""
54
+ for response in responses:
55
+ response_text += response.text
56
+
57
+ return response_text if response_text else "No valid response generated or response was blocked."
58
+
59
+ except Exception as e:
60
+ return str(e)
61
+
62
+ # JavaScript to copy text to clipboard
63
+ copy_js = """
64
+ function copyText() {
65
+ const outputText = document.querySelector('textarea').value;
66
+ navigator.clipboard.writeText(outputText).then(function() {
67
+ alert('Text copied to clipboard');
68
+ }, function(err) {
69
+ alert('Failed to copy text: ', err);
70
+ });
71
+ }
72
+ """
73
+
74
+ # Gradio interface
75
+ with gr.Blocks() as iface:
76
+ input_text = gr.Textbox(lines=2, placeholder="Enter text here...")
77
+ output_text = gr.Textbox(lines=5, interactive=False)
78
+ copy_button = gr.Button("Copy Output Text")
79
+
80
+ def copy_to_clipboard():
81
+ return gr.update(_js="copyText()")
82
+
83
+ input_text.change(generate, inputs=input_text, outputs=output_text)
84
+ copy_button.click(copy_to_clipboard)
85
+
86
+ gr.Markdown(f"<script>{copy_js}</script>")
87
+
88
+ gr.Markdown("<h1>Chuunibyou Text Generator</h1>")
89
+ gr.Markdown("Transform text into an elaborate and formal style with a nobleman tone.")
90
+
91
+ if __name__ == "__main__":
92
+ iface.launch()