import streamlit as st import pandas as pd import os import requests st.set_page_config(page_icon='🦜', page_title='Text Generation Labeling Tool', layout='wide', initial_sidebar_state="collapsed") st.markdown("
Your distractors:
", unsafe_allow_html=True) col_21, col_22 = st.columns(spec=[9, 1]) txt_distractors = col_21.text_area(height=90, label='Your distractors:', label_visibility='collapsed', value=df['distract'][st.session_state.idx]) btn_generate_distractor = col_22.button(label='Generate distractors', use_container_width=True) if btn_generate_distractor: if filename_input == 'BiologyQA.csv': expert = 'biologist' elif filename_input == 'GeographyQA.csv': expert = 'geographer' elif filename_input == 'HistoryQA.csv': expert = 'historian' url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent' headers = {'Content-Type': 'application/json'} data = { 'contents': [ { 'parts': [ { 'text': f"You are a great {expert}, here is the following content: context: '{txt_context}', question: '{txt_question}', answer: '{txt_answer}' generate three distract answers. Distractor answers are separated by [SEP]. Example: Distract answer 1 [SEP] Distract answer 2 [SEP] Distract answer 3" } ] } ] } api_key = 'AIzaSyApFAbCUA1H-VHAidzqmyStHFe92ODeO1Y' params = {'key': api_key} response = requests.post(url, headers=headers, json=data, params=params) if response.status_code == 200: correct = response.json()['candidates'][0]['content']['parts'][0]['text'] st.success(f'3 distraction answers: {correct}') st.cache_data.clear() else: st.error('Failed to generate distractors. Please check API and inputs.') st.rerun() if btn_previous: if st.session_state.idx > 0: st.session_state.idx -= 1 st.rerun() else: pass if btn_next: if st.session_state.idx < (len(df) - 1): st.session_state.idx += 1 st.rerun() else: pass if btn_save: df['context'][st.session_state.idx] = txt_context df['question'][st.session_state.idx] = txt_question df['answer'][st.session_state.idx] = txt_answer df['distract'][st.session_state.idx] = txt_distractors df['question_type'][st.session_state.idx] = revert_question_type_id(txt_question_type) df.to_csv(f'./Datasets/{filename_input}', index=None) if btn_goto: st.session_state.idx = txt_goto - 1 st.rerun()