import gradio as gr import requests import pandas as pd import io import os # Get authentication credentials from environment variables #USERNAME = os.getenv("GRADIO_USERNAME", "admin") #PASSWORD = os.getenv("GRADIO_PASSWORD", "my_secure_password") # Function to analyze tesis and return the results def analyze_tesis(tesis_code): url = "http://tesis-analiz.rise-consulting.net/analyze_tesis" payload = {"tesis": tesis_code} response = requests.post(url, json=payload) if response.status_code == 200: json_response = response.json() analysis_text = json_response.get('analysis', "Analiz mevcut değil.") summary_text = json_response.get('summary', "Özet mevcut değil.") result_df = pd.DataFrame(json_response.get('result_df', [])) risk_df = pd.DataFrame(json_response.get('risk_df', [])) return analysis_text, summary_text, result_df, risk_df else: return f"Hata: {response.status_code}, {response.text}", "", pd.DataFrame(), pd.DataFrame() # Function to export dataframe to CSV and return as downloadable file def export_csv(df, filename): csv_buffer = io.StringIO() df.to_csv(csv_buffer, index=False) csv_buffer.seek(0) with open(filename, "w", encoding="utf-8") as f: f.write(csv_buffer.getvalue()) return gr.File(value=filename, visible=True) # Creating the Gradio Interface with gr.Blocks() as demo: gr.Markdown(""" # Tesis Analiz Uygulaması Aşağıya bir tesis kodu girerek analiz, özet ve detaylı raporu alın. """) with gr.Row(): tesis_input = gr.Textbox(label="Tesis Kodu", placeholder="Tesis kodunu girin (ör. B40JA110)") analyze_button = gr.Button("Tesis Analiz Et") with gr.Row(): analysis_output = gr.Textbox(label="Analiz", lines=10) summary_output = gr.Textbox(label="Özet", lines=10) with gr.Row(): result_output = gr.Dataframe(label="Sonuç Detayları") risk_output = gr.Dataframe(label="Risk Detayları") with gr.Row(): result_download_button = gr.Button("Sonuç Detaylarını CSV olarak İndir") risk_download_button = gr.Button("Risk Detaylarını CSV olarak İndir") result_csv = gr.File(interactive=False, visible=False) risk_csv = gr.File(interactive=False, visible=False) analyze_button.click(analyze_tesis, inputs=tesis_input, outputs=[analysis_output, summary_output, result_output, risk_output]) result_download_button.click(lambda tesis_code: export_csv(analyze_tesis(tesis_code)[2], "sonuc_detaylari.csv"), inputs=tesis_input, outputs=result_csv) risk_download_button.click(lambda tesis_code: export_csv(analyze_tesis(tesis_code)[3], "risk_detaylari.csv"), inputs=tesis_input, outputs=risk_csv) # Launching the Gradio app with authentication demo.launch()