| import streamlit as st |
| import pandas as pd |
| import openai |
| import difflib |
|
|
| st.set_page_config(layout="wide") |
|
|
| |
| st.title("Agent de Correction de Code et Analyse de Données") |
|
|
| |
| df = pd.read_csv('brvehins1a.csv', sep=',') |
| st.write("### Données chargées :") |
| st.write(df) |
|
|
| |
| openai.api_key = '....' |
|
|
| |
| user_input = st.text_area("Collez votre code ici :", height=200) |
|
|
| |
| def get_code_correction(code): |
| response = openai.ChatCompletion.create( |
| model="gpt-3.5-turbo", |
| messages=[ |
| {"role": "user", "content": f"Corrige ce code et explique les changements :\n\n{code}"} |
| ] |
| ) |
| return response.choices[0].message['content'] |
|
|
| |
| def display_diff(original, corrected): |
| diff = difflib.ndiff(original.splitlines(), corrected.splitlines()) |
| formatted_diff = [] |
| for line in diff: |
| if line.startswith('+'): |
| formatted_diff.append(f"<span style='color: green;'>{line}</span>") |
| elif line.startswith('-'): |
| formatted_diff.append(f"<span style='color: yellow;'>{line}</span>") |
| else: |
| formatted_diff.append(line) |
| return "<br>".join(formatted_diff) |
|
|
| |
| query_input = st.text_input("Posez une question sur les données :", "") |
|
|
| |
| if user_input: |
| with st.spinner("Correction en cours..."): |
| corrected_code = get_code_correction(user_input) |
| st.write("### Code corrigé :") |
| st.code(corrected_code, language='python') |
|
|
| st.write("### Comparaison :") |
| diff_output = display_diff(user_input, corrected_code) |
| st.markdown(diff_output, unsafe_allow_html=True) |
|
|
| st.write("### Explication des changements :") |
| explanation = get_code_correction( |
| f"Explique les changements apportés à ce code :\n\n{user_input}\n\nCode corrigé :\n{corrected_code}") |
| st.write(explanation) |
|
|
| |
| if query_input: |
| with st.spinner("Analyse des données en cours..."): |
| data_summary = get_code_correction( |
| f"Voici un DataFrame : {df.head().to_json()}. {query_input}" |
| ) |
| st.write("### Réponse à votre question :") |
| st.write(data_summary) |
|
|