Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import threading
|
| 3 |
+
from flask import Flask, request, jsonify
|
| 4 |
+
from pyngrok import ngrok, conf
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# Flask app setup
|
| 8 |
+
app = Flask(__name__)
|
| 9 |
+
|
| 10 |
+
def xor_encrypt_decrypt(text, key):
|
| 11 |
+
"""
|
| 12 |
+
Encrypts or decrypts text using XOR with the given key.
|
| 13 |
+
:param text: The input text to encrypt/decrypt.
|
| 14 |
+
:param key: The key to use for XOR.
|
| 15 |
+
:return: The encrypted/decrypted text.
|
| 16 |
+
"""
|
| 17 |
+
return ''.join(chr(ord(c) ^ key) for c in text)
|
| 18 |
+
|
| 19 |
+
@app.route('/decrypt', methods=['GET'])
|
| 20 |
+
def decrypt():
|
| 21 |
+
try:
|
| 22 |
+
encrypted_text = request.args.get('encrypted_text')
|
| 23 |
+
key = request.args.get('key', type=int)
|
| 24 |
+
|
| 25 |
+
if not encrypted_text or key is None:
|
| 26 |
+
return jsonify({"error": "Invalid input. Both 'encrypted_text' and 'key' are required."}), 400
|
| 27 |
+
|
| 28 |
+
decrypted_text = xor_encrypt_decrypt(encrypted_text, key)
|
| 29 |
+
return jsonify({"data": decrypted_text}), 200
|
| 30 |
+
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return jsonify({"error": str(e)}), 500
|
| 33 |
+
|
| 34 |
+
# Start Flask app with ngrok
|
| 35 |
+
def run_flask():
|
| 36 |
+
authtoken = os.getenv("NGROK_AUTHTOKEN")
|
| 37 |
+
if not authtoken:
|
| 38 |
+
raise ValueError("NGROK_AUTHTOKEN environment variable is not set.")
|
| 39 |
+
|
| 40 |
+
conf.get_default().auth_token = authtoken
|
| 41 |
+
public_url = ngrok.connect(8080).public_url
|
| 42 |
+
print(f"Server is publicly available at: {public_url}")
|
| 43 |
+
app.run(port=8080)
|
| 44 |
+
|
| 45 |
+
# Gradio app setup
|
| 46 |
+
def dummy_function(text):
|
| 47 |
+
return text
|
| 48 |
+
|
| 49 |
+
def run_gradio():
|
| 50 |
+
with gr.Blocks() as demo:
|
| 51 |
+
gr.Markdown("### Dummy Gradio Interface")
|
| 52 |
+
text_input = gr.Textbox(label="Input Text")
|
| 53 |
+
text_output = gr.Textbox(label="Output Text")
|
| 54 |
+
submit_button = gr.Button("Submit")
|
| 55 |
+
|
| 56 |
+
submit_button.click(dummy_function, inputs=text_input, outputs=text_output)
|
| 57 |
+
|
| 58 |
+
demo.launch(share=True)
|
| 59 |
+
|
| 60 |
+
# Run Flask and Gradio in parallel
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
flask_thread = threading.Thread(target=run_flask)
|
| 63 |
+
flask_thread.daemon = True
|
| 64 |
+
flask_thread.start()
|
| 65 |
+
|
| 66 |
+
run_gradio()
|