sohoso commited on
Commit
d4fc0d8
·
verified ·
1 Parent(s): 7813a87

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -43
app.py CHANGED
@@ -102,48 +102,33 @@ def export_to_file(output_text):
102
  file.write(output_text)
103
  return "Output exported to output.txt"
104
 
105
- def process_image(image, prompt):
106
- response = ImageChat(image, prompt)
107
- return response
108
-
109
- # Custom JS for copy and export buttons
110
- copy_js = """
111
- async function copyOutput() {
112
- const outputElement = document.getElementById('output');
113
- const text = outputElement.innerText;
114
- await navigator.clipboard.writeText(text);
115
- alert('Copied to clipboard!');
116
- }
117
- """
118
-
119
- export_js = """
120
- function exportOutput() {
121
- const outputElement = document.getElementById('output');
122
- const text = outputElement.innerText;
123
- const blob = new Blob([text], { type: 'text/plain' });
124
- const url = URL.createObjectURL(blob);
125
- const a = document.createElement('a');
126
- a.href = url;
127
- a.download = 'output.txt';
128
- document.body.appendChild(a);
129
- a.click();
130
- document.body.removeChild(a);
131
- }
132
- """
133
-
134
- app = gr.Interface(
135
- fn=process_image,
136
- inputs=[gr.Image(type="pil", label=""), gr.Textbox(label="Your Prompt", value="Analyze")],
137
- outputs=[gr.HTML(label="Response", elem_id="output")],
138
- title="",
139
- theme=gr.themes.Soft()
140
- )
141
-
142
- app.launch(share=False, inline=True, server_name="0.0.0.0", server_port=7860)
143
-
144
- # Include the buttons and custom JS in the launch method
145
  with app:
146
- gr.Button("Copy").click(None, None, None, _js=copy_js)
147
- gr.Button("Export").click(None, None, None, _js=export_js)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
 
149
- app.launch()
 
102
  file.write(output_text)
103
  return "Output exported to output.txt"
104
 
105
+ app = gr.Blocks()
106
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  with app:
108
+ with gr.Column():
109
+ image_input = gr.Image(type="pil", label="")
110
+ prompt_input = gr.Textbox(label="Your Prompt", value="Analyze")
111
+ output_field = gr.Markdown(label="Response")
112
+
113
+ with gr.Row():
114
+ copy_button = gr.Button("Copy")
115
+ export_button = gr.Button("Export")
116
+
117
+ def process_image(image, prompt):
118
+ response = ImageChat(image, prompt)
119
+ return response
120
+
121
+ def copy_callback(output_text):
122
+ import pyperclip
123
+ pyperclip.copy(output_text)
124
+ return "Copied to clipboard!"
125
+
126
+ def export_callback(output_text):
127
+ with open("output.txt", "w") as file:
128
+ file.write(output_text)
129
+ return "Output exported to output.txt"
130
+
131
+ copy_button.click(fn=copy_callback, inputs=[output_field], outputs=[])
132
+ export_button.click(fn=export_callback, inputs=[output_field], outputs=[])
133
 
134
+ app.launch()