from openai import OpenAI import streamlit as st from streamlit import session_state import numpy as np client = OpenAI() openai.api_key = os.getenv("OPENAI_API_KEY") from sentence_transformers import SentenceTransformer, LoggingHandler, losses, util, InputExample model = SentenceTransformer('paraphrase-MiniLM-L6-v2') from scipy import spatial def cosine_similarity(x,y): return 1 - spatial.distance.cosine(x,y) def scorer(m,s): response = client.chat.completions.create( model="gpt-4", messages=[ { "role": "system", "content": "You are a Grading assistant. Your task is to give marks to the student answers based on the model answer. You will be provided student answer and model answer. Please check both carefully and give marks out of 10. \n\n<>\nThese answers are written by High school kids. Don't be too strict. Give marks generously. \nGive marks in the range of 0.5. \nIf the model answer and student answer is slightly matching then give marks generously. Please give output in json format like this:\n{\"output\":5}" }, { "role": "user", "content": f"Model answer: {m}"}, { "role": "user", "content": f"Student answer: {s}" } ], temperature=0, max_tokens=256, top_p=1, frequency_penalty=0, presence_penalty=0 ) return response.choices[0].message.content def embeddings_cosine(s1,s2,model): embeddings1 = model.encode(s1) embeddings2 = model.encode(s2) cosine_scores = cosine_similarity(embeddings1, embeddings2) return cosine_scores def main_score(m,s): if len(s.split())>15: body_score = embeddings_cosine(preprocess_bert(m),preprocess_bert(s),model) score = body_score*10 return np.abs(np.round(score * 2) / 2) else: score = scorer(m,s) return score from st_pages import Page, Section, show_pages, add_page_title,add_indentation st.set_page_config(page_title="Auto score Openai", page_icon="📈") st.markdown("

Welcome to Our App! 👋

", unsafe_allow_html=True) if 'result' not in session_state: session_state['result']= "" st.title("Auto score") text1= st.text_area(label= "Please write the text bellow", placeholder="What does the tweet say?") text2= st.text_area(label= "Please write the text bellow", placeholder="What does the tweet say?") def classify(text): session_state['result'] = main_score(text1,text2) st.text_area("result", value=session_state['result']) st.button("Classify", on_click=classify, args=[text1,text2])