themeetjani commited on
Commit
585b37a
β€’
1 Parent(s): 9fb361b

Update pages/Our_model.py

Browse files
Files changed (1) hide show
  1. pages/Our_model.py +68 -0
pages/Our_model.py CHANGED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from openai import OpenAI
2
+ import streamlit as st
3
+ from streamlit import session_state
4
+ import numpy as np
5
+ client = OpenAI()
6
+ openai.api_key = os.getenv("OPENAI_API_KEY")
7
+ from sentence_transformers import SentenceTransformer, LoggingHandler, losses, util, InputExample
8
+ model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
9
+ from scipy import spatial
10
+ def cosine_similarity(x,y):
11
+ return 1 - spatial.distance.cosine(x,y)
12
+ def scorer(m,s):
13
+ response = client.chat.completions.create(
14
+ model="gpt-4",
15
+ messages=[
16
+ {
17
+ "role": "system",
18
+ "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<<REMEMBER>>\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}"
19
+ },
20
+ {
21
+ "role": "user",
22
+ "content": f"Model answer: {m}"},
23
+ {
24
+ "role": "user",
25
+ "content": f"Student answer: {s}"
26
+ }
27
+ ],
28
+ temperature=0,
29
+ max_tokens=256,
30
+ top_p=1,
31
+ frequency_penalty=0,
32
+ presence_penalty=0
33
+ )
34
+ return response.choices[0].message.content
35
+ def embeddings_cosine(s1,s2,model):
36
+ embeddings1 = model.encode(s1)
37
+ embeddings2 = model.encode(s2)
38
+ cosine_scores = cosine_similarity(embeddings1, embeddings2)
39
+ return cosine_scores
40
+ def main_score(m,s):
41
+ if len(s.split())>15:
42
+ body_score = embeddings_cosine(preprocess_bert(m),preprocess_bert(s),model)
43
+ score = body_score*10
44
+ return np.abs(np.round(score * 2) / 2)
45
+ else:
46
+ score = scorer(m,s)
47
+ return score
48
+
49
+ from st_pages import Page, Section, show_pages, add_page_title,add_indentation
50
+ st.set_page_config(page_title="Auto score Openai", page_icon="πŸ“ˆ")
51
+
52
+ st.markdown("<h1 style='text-align: center; color: black;'> Welcome to Our App! πŸ‘‹</h1>", unsafe_allow_html=True)
53
+
54
+ if 'result' not in session_state:
55
+ session_state['result']= ""
56
+
57
+ st.title("Auto score")
58
+ text1= st.text_area(label= "Please write the text bellow",
59
+ placeholder="What does the tweet say?")
60
+ text2= st.text_area(label= "Please write the text bellow",
61
+ placeholder="What does the tweet say?")
62
+ def classify(text):
63
+ session_state['result'] = main_score(text1,text2)
64
+
65
+
66
+ st.text_area("result", value=session_state['result'])
67
+
68
+ st.button("Classify", on_click=classify, args=[text1,text2])