Spaces:
Paused
Paused
| import os | |
| import threading | |
| from flask import Flask, request, jsonify | |
| from pyngrok import ngrok, conf | |
| import gradio as gr | |
| # Flask app setup | |
| app = Flask(__name__) | |
| def xor_encrypt_decrypt(text, key): | |
| """ | |
| Encrypts or decrypts text using XOR with the given key. | |
| :param text: The input text to encrypt/decrypt. | |
| :param key: The key to use for XOR. | |
| :return: The encrypted/decrypted text. | |
| """ | |
| return ''.join(chr(ord(c) ^ key) for c in text) | |
| def decrypt(): | |
| try: | |
| encrypted_text = request.args.get('encrypted_text') | |
| key = request.args.get('key', type=int) | |
| if not encrypted_text or key is None: | |
| return jsonify({"error": "Invalid input. Both 'encrypted_text' and 'key' are required."}), 400 | |
| decrypted_text = xor_encrypt_decrypt(encrypted_text, key) | |
| return jsonify({"data": decrypted_text}), 200 | |
| except Exception as e: | |
| return jsonify({"error": str(e)}), 500 | |
| # Start Flask app with ngrok | |
| def run_flask(): | |
| authtoken = os.getenv("NGROK_AUTHTOKEN") | |
| if not authtoken: | |
| raise ValueError("NGROK_AUTHTOKEN environment variable is not set.") | |
| conf.get_default().auth_token = authtoken | |
| public_url = ngrok.connect(8080).public_url | |
| print(f"Server is publicly available at: {public_url}") | |
| app.run(port=8080) | |
| # Gradio app setup | |
| def dummy_function(text): | |
| return text | |
| def run_gradio(): | |
| with gr.Blocks() as demo: | |
| gr.Markdown("### Dummy Gradio Interface") | |
| text_input = gr.Textbox(label="Input Text") | |
| text_output = gr.Textbox(label="Output Text") | |
| submit_button = gr.Button("Submit") | |
| submit_button.click(dummy_function, inputs=text_input, outputs=text_output) | |
| demo.launch(share=True) | |
| # Run Flask and Gradio in parallel | |
| if __name__ == "__main__": | |
| flask_thread = threading.Thread(target=run_flask) | |
| flask_thread.daemon = True | |
| flask_thread.start() | |
| run_gradio() | |