본문 바로가기
LLM

[ChatGPT][flask][tutorial]ChatGPT, flask app Zero shot tutorial

by Chandler.j 2024. 7. 31.
반응형

1. 튜토리얼 및 준비물

- ChatGPT를 활용해 zero shot으로 간단한 앱 만들어보기

- QA 에 대한 평가 지표를 비교하고 보고서를 만들어 word 파일로 저장시키는 앱

- 준비물 두 시스템에 대한 평가 지표

- 사용된 평가지표

1) answer_similarity

2) answer_correctness


- sample_rag: RAG 시스템에 대한 평가표

- sample_default: 기본 GPT 시스템에 대한 평가표

sample_rag.csv
0.00MB
sample_default.csv
0.00MB

 

 

 


2. Zero Shot 쿼리 짜기

첨부하는 두개의 성능 결과 csv 파일을 업로드 하고 비교평가 하여 word 파일로 저장시키는 flask 앱 만들어줘.

참고 사항은 아래와 같아

---
question은 동일해 따라서 해당 칼럼이 key야.

---
answer_similarity와 answer_correctness가 평가 지표야
두개의 평균값과 표준편차 를 구하고 p-value 확인하여 통계적 유의성을 알려줘

---
보고서 형식은 아래와 같아
1. 통계 목적 및 방법
2. 통계 분석 결과
3. 결론

---
웹 페이지는 세련되고 유저 친화적인 디자인으로 코딩해줘

- app.py

from flask import Flask, request, render_template, send_file
import pandas as pd
from scipy import stats
from docx import Document
import os

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/upload', methods=['POST'])
def upload_file():
    if 'file1' not in request.files or 'file2' not in request.files:
        return "Please upload both files", 400

    file1 = request.files['file1']
    file2 = request.files['file2']
    
    df1 = pd.read_csv(file1)
    df2 = pd.read_csv(file2)

    report_path = perform_analysis(df1, df2)

    return send_file(report_path, as_attachment=True)

def perform_analysis(df1, df2):
    # Ensure both dataframes have the same questions
    if not all(df1['question'] == df2['question']):
        raise ValueError("Questions do not match between files.")

    # Calculate statistics
    result = {}
    for metric in ['answer_similarity', 'answer_correctness']:
        mean1 = df1[metric].mean()
        mean2 = df2[metric].mean()
        std1 = df1[metric].std()
        std2 = df2[metric].std()
        t_stat, p_val = stats.ttest_ind(df1[metric], df2[metric])

        result[metric] = {
            'mean1': mean1, 'mean2': mean2,
            'std1': std1, 'std2': std2,
            'p_val': p_val
        }

    # Create Word report
    doc = Document()
    doc.add_heading('Statistical Analysis Report', 0)

    doc.add_heading('1. Purpose and Method', level=1)
    doc.add_paragraph(
        'The purpose of this analysis is to compare the performance of two models '
        'based on the metrics of answer similarity and answer correctness. The datasets '
        'contain answers to identical questions. The analysis includes calculating the mean, '
        'standard deviation, and performing a t-test to assess statistical significance.'
    )

    doc.add_heading('2. Statistical Analysis Results', level=1)
    for metric, values in result.items():
        doc.add_heading(metric, level=2)
        doc.add_paragraph(
            f'Mean (File 1): {values["mean1"]}\n'
            f'Mean (File 2): {values["mean2"]}\n'
            f'Standard Deviation (File 1): {values["std1"]}\n'
            f'Standard Deviation (File 2): {values["std2"]}\n'
            f'p-value: {values["p_val"]}'
        )

    doc.add_heading('3. Conclusion', level=1)
    conclusion = 'Based on the p-values, we can conclude that:'
    for metric, values in result.items():
        significance = "statistically significant" if values["p_val"] < 0.05 else "not statistically significant"
        conclusion += f'\n- The difference in {metric} is {significance}.'

    doc.add_paragraph(conclusion)

    report_path = '/mnt/data/statistical_analysis_report.docx'
    doc.save(report_path)
    
    return report_path

if __name__ == '__main__':
    app.run(debug=True)

- templates/index.html

<!doctype html>
<html lang="en">
  <head>
    <title>Statistical Analysis Upload</title>
    <style>
      body {
        font-family: Arial, sans-serif;
        margin: 0;
        padding: 0;
        background: #f4f4f4;
      }
      .container {
        max-width: 600px;
        margin: 50px auto;
        padding: 20px;
        background: #fff;
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
      }
      h1 {
        text-align: center;
        margin-bottom: 20px;
      }
      input[type="file"] {
        display: block;
        margin-bottom: 10px;
      }
      button {
        display: block;
        width: 100%;
        padding: 10px;
        background: #007bff;
        color: #fff;
        border: none;
        cursor: pointer;
      }
      button:hover {
        background: #0056b3;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <h1>Upload CSV Files</h1>
      <form action="/upload" method="post" enctype="multipart/form-data">
        <input type="file" name="file1" required>
        <input type="file" name="file2" required>
        <button type="submit">Submit</button>
      </form>
    </div>
  </body>
</html>

3. 결과 폴더 생성 및 실행

- 디렉토리 생성해서 script 들 생성


- 코드 실행

python app.py

 

 


- 해당 페이지 접속 후 파일 업로드 하여 다운로드


- 결과 보고서 다운

statistical_analysis_report.docx
0.04MB

 


TOP

Designed by 티스토리