prabinpanta0 commited on
Commit
f005be3
1 Parent(s): 2a0c386

Update app1.py

Browse files
Files changed (1) hide show
  1. app1.py +29 -26
app1.py CHANGED
@@ -4,6 +4,7 @@ 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")
@@ -59,34 +60,36 @@ def generate(text):
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()
 
4
  from vertexai.generative_models import GenerativeModel
5
  import vertexai.preview.generative_models as generative_models
6
  import gradio as gr
7
+ import pyperclip
8
 
9
  # Read the service account key JSON file path from environment variable
10
  SERVICE_ACCOUNT_KEY_PATH = os.getenv("GOOGLE_APPLICATION_CREDENTIALS")
 
60
  except Exception as e:
61
  return str(e)
62
 
63
+ def copy_to_clipboard(text):
64
+ pyperclip.copy(text)
65
+ return "Text copied to clipboard!"
 
 
 
 
 
 
 
 
66
 
67
+ def update_output_and_copy(text):
68
+ response_text = generate(text)
69
+ pyperclip.copy(response_text)
70
+ return gr.update(value=response_text, visible=True), "Text copied to clipboard!"
 
 
 
 
 
 
 
71
 
72
+ iface = gr.Interface(
73
+ fn=generate,
74
+ inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
75
+ outputs=gr.Textbox(visible=True),
76
+ title="Chuunibyou Text Generator",
77
+ description="Transform text into an elaborate and formal style with a Chuunibyou tone."
78
+ )
79
 
80
+ iface_with_button = gr.Blocks()
81
+
82
+ with iface_with_button:
83
+ textbox = gr.Textbox(lines=2, placeholder="Enter text here...")
84
+ output = gr.Textbox(visible=True)
85
+ submit_button = gr.Button("Submit")
86
+ copy_message = gr.Textbox(visible=True)
87
+
88
+ submit_button.click(
89
+ fn=update_output_and_copy,
90
+ inputs=textbox,
91
+ outputs=[output, copy_message]
92
+ )
93
 
94
  if __name__ == "__main__":
95
+ iface_with_button.launch()