Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from src.bb84 import BB84Protocol | |
| from src.e91 import E91Protocol | |
| from src.circuit_viz import CircuitVisualizer | |
| import numpy as np | |
| import uuid | |
| class QuantumCryptApp: | |
| """Interface gráfica para o QuantumCrypt usando Gradio.""" | |
| def __init__(self): | |
| self.bb84 = BB84Protocol() | |
| self.e91 = E91Protocol() | |
| def create_interface(self): | |
| """Cria a interface Gradio.""" | |
| with gr.Blocks(title="QuantumCrypt", css="footer{display:none !important}") as interface: | |
| gr.Markdown("# QuantumCrypt") | |
| with gr.Tabs(): | |
| with gr.Tab("BB84"): | |
| with gr.Row(): | |
| with gr.Column(): | |
| key_length = gr.Slider( | |
| minimum=8, maximum=256, value=64, | |
| label="Tamanho da Chave" | |
| ) | |
| error_correction = gr.Checkbox( | |
| label="Usar Correção de Erros", | |
| visible=False | |
| ) | |
| generate_btn = gr.Button("Gerar Chave BB84") | |
| with gr.Column(): | |
| output_text = gr.TextArea(label="Resultado") | |
| circuit_viz = gr.Image(label="Visualização do Circuito", | |
| show_download_button=False, | |
| show_fullscreen_button=False, | |
| show_share_button=False) | |
| with gr.Tab("E91"): | |
| with gr.Row(): | |
| with gr.Column(): | |
| key_length_e91 = gr.Slider( | |
| minimum=8, maximum=256, value=64, | |
| label="Tamanho da Chave" | |
| ) | |
| error_correction_e91 = gr.Checkbox( | |
| label="Usar Correção de Erros", | |
| visible=False | |
| ) | |
| generate_btn_e91 = gr.Button("Gerar Chave BB84") | |
| with gr.Column(): | |
| output_text_e91 = gr.TextArea(label="Resultado") | |
| circuit_viz_e91 = gr.Image(label="Visualização do Circuito", | |
| show_download_button=False, | |
| show_fullscreen_button=False, | |
| show_share_button=False) | |
| with gr.Accordion(label="About", open=False): | |
| with open("README.md", "r") as readme_file: | |
| gr.Markdown( | |
| readme_file.read() | |
| ) | |
| def generate_bb84(length, use_correction): | |
| filename = f"data/{uuid.uuid4()}.png" | |
| protocol = BB84Protocol( | |
| key_length=length, | |
| error_correction=use_correction | |
| ) | |
| result = protocol.generate_key() | |
| # Gerar visualização | |
| circuit_img = CircuitVisualizer.draw_circuit( | |
| protocol.current_circuit, | |
| filename=filename | |
| ) | |
| # Formatar resultado | |
| output = ( | |
| f"Chave Alice: {''.join(map(str, result['alice']))}\n" | |
| f"Chave Bob: {''.join(map(str, result['bob']))}\n" | |
| f"Tamanho: {len(result['alice'])} bits\n" | |
| f"Correção de Erros: {'Ativada' if use_correction else 'Desativada'}" | |
| ) | |
| return output, filename | |
| def generate_e91(length): | |
| filename = f"data/{uuid.uuid4()}.png" | |
| protocol = E91Protocol( | |
| key_length=length | |
| ) | |
| result = protocol.generate_key() | |
| # Gerar visualização | |
| circuit_img = CircuitVisualizer.draw_circuit( | |
| protocol.current_circuit, | |
| filename=filename | |
| ) | |
| # Formatar resultado | |
| output = ( | |
| f"Chave Alice: {''.join(map(str, result['alice']))}\n" | |
| f"Chave Bob: {''.join(map(str, result['bob']))}\n" | |
| f"Tamanho: {len(result['alice'])} bits\n" | |
| ) | |
| return output, filename | |
| generate_btn_e91.click( | |
| generate_e91, | |
| inputs=[key_length_e91], | |
| outputs=[output_text_e91, circuit_viz_e91] | |
| ) | |
| generate_btn.click( | |
| generate_bb84, | |
| inputs=[key_length, error_correction], | |
| outputs=[output_text, circuit_viz] | |
| ) | |
| return interface | |
| # Exemplo de uso | |
| if __name__ == "__main__": | |
| app = QuantumCryptApp() | |
| interface = app.create_interface() | |
| interface.launch() |