Spaces:
Runtime error
Runtime error
| import os | |
| import guidance | |
| import json | |
| import gradio as gr | |
| # import googleapiclient.discovery | |
| import requests | |
| from bs4 import BeautifulSoup | |
| import re | |
| from requests.exceptions import RequestException | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| # my_cse_id = "02dc218e5cd174d7e" | |
| # my_cse_engine = googleapiclient.discovery.build("customsearch", "v1", developerKey="AIzaSyAuktiM0lPtA2gqr4WJdULB3PrsblDejZY") | |
| # # os.environ['OPENAI_API_KEY'] = os.environ.get('OPEN_API_KEY') | |
| # def google_search(search_term): | |
| # res = my_cse_engine.cse().list( | |
| # q=search_term, | |
| # cx=my_cse_id, | |
| # num=6, | |
| # ).execute() | |
| # return res['items'] | |
| # results = google_search("Explain about Java Programming") | |
| # def gen_ans(link, timeout=5): | |
| # try: | |
| # response = requests.get(link, timeout=timeout) | |
| # response.raise_for_status() # Raise an exception for non-successful responses | |
| # soup = BeautifulSoup(response.content, 'html.parser') | |
| # answer = soup.find_all('p') | |
| # cut_ans = str(answer) | |
| # return cut_ans | |
| # except (RequestException, requests.Timeout, requests.ConnectionError) as e: | |
| # print(f"An error occurred while processing the link: {link}") | |
| # print(f"Error details: {str(e)}") | |
| # return "time out for extraction" | |
| # def clean(text): | |
| # # makes text devoid of tags and encoded characters | |
| # # uses BeautifulSoup4 and Regular Expression | |
| # soup = BeautifulSoup(text, "html.parser").get_text() | |
| # encoded_bytes = soup.encode('utf-8') | |
| # cleaned_text = encoded_bytes.decode('unicode_escape', 'ignore') | |
| # return (re.sub(r'\W\s+',' ',soup)).strip('][') | |
| # def top_10_ans(ques): | |
| # results = google_search(ques) | |
| # answers = [] | |
| # k=0 | |
| # for i in results: | |
| # l=gen_ans(i['link']) | |
| # if l!='time out for extraction': | |
| # answers.append(clean(l)) | |
| # k+=1 | |
| # if k==3: | |
| # break | |
| # return answers | |
| # def summarize(question,l): | |
| # model = guidance.llms.OpenAI('gpt-3.5-turbo') | |
| # system = guidance(''' | |
| # {{#system~}} | |
| # You are a text-summarizer Expert System. You are given with multiple long paragraphs of text data and a question based on the given data. | |
| # You are required to summarize the content all user input long text paragraphs to a short essay. | |
| # The essay must have the complete information of all the input text paragraphs. | |
| # {{~/system}} | |
| # {{#user~}} | |
| # Now, I'll send multiple long text paragraphs, question based on the given paragraph's to you and you need to generate a summary of all the paragraphs to a | |
| # single essay of 190 to 300 words and the complete intent in my paragraphs must be reflected in the final generated summary, based on the question given. | |
| # {{~/user}} | |
| # {{#user~}} | |
| # {{question}} | |
| # {{~/user}} | |
| # {{#user~}} | |
| # {{para1}} | |
| # {{~/user}} | |
| # {{#user~}} | |
| # {{para2}} | |
| # {{~/user}} | |
| # {{#user~}} | |
| # {{para3}} | |
| # {{~/user}} | |
| # {{#user~}} | |
| # Now, based on the past five input paragraphic texts and question given, generate a summary essay of 190 to 300 words enveloping the complete intent | |
| # of the given paragraphs. | |
| # {{~/user}} | |
| # {{#assistant~}} | |
| # {{gen 'summary' temperature=0.5 max_tokens=1000}} | |
| # {{~/assistant}} | |
| # ''', llm = model) | |
| # output = system(question=question,para1=str(l[0][:2000]), para2=str(l[1][:2000]), para3=str(l[2][:2000])) | |
| # return output.get('summary') | |
| def guideus(question, answer, essay): | |
| gptThreeFive = guidance.llms.OpenAI('gpt-3.5-turbo') | |
| mySys = guidance(''' | |
| {{#system~}} | |
| You are an Interviewee answer grading System. You have highest precision in grading the answer. Grade the answers given by an interviewee. | |
| Scenario : | |
| 1. The Interviewee is given the question to read for 3 minutes. | |
| 2. They were later provided with listening conceptual essay. He is allowed to take notes on the comphrehension. | |
| 3. For the next 20 minutes, they are instructed to complete their answer based on their notes and the question asked. | |
| 4. The response is then graded according to appropriate graded measures and pattern scoring. | |
| {{~/system}} | |
| {{#user~}} | |
| Your task now is to assess an interviewee's response. | |
| The question provided to the interviewee is as follows : | |
| {{question}} | |
| Now consider the following grading measures and their definitions. | |
| Grading Measures: | |
| Clear Introduction: The answer should have a clear introduction that introduces the topic intended in the question. | |
| Coherent Body Paragraphs: The answer should have well-developed body paragraphs that support the question with relevant examples, details, and explanations. | |
| Logical Organization: The answer should demonstrate a logical progression of ideas and use appropriate transitions between paragraphs and sentences.\ | |
| Cohesion: The answer should have coherence in terms of ideas, with connections between sentences and paragraphs clearly established. | |
| Grammar and Sentence Structure: The answer should demonstrate a good command of grammar, with accurate sentence structures and appropriate use of verb tenses, pronouns, and modifiers. | |
| Vocabulary: The answer should use a wide range of vocabulary appropriately and effectively to convey meaning and enhance the quality of the writing. | |
| Idiomatic Language: The answer should demonstrate the ability to use idiomatic expressions and phrases in a natural and accurate way. | |
| Word Choice: The answer should show precision and accuracy in word choice, avoiding repetition and using words that convey the intended meaning. | |
| Relevance to the Topic: The answer should address the given topic directly and provide a focused and relevant response. | |
| Content Accuracy: The answer should demonstrate a good understanding of the topic and present accurate information and examples. | |
| Use of Examples and Details: The answer should provide specific examples and details to support ideas and arguments effectively. | |
| Critical Thinking: The answer should show the candidate's ability to analyze and evaluate the topic, presenting a well-reasoned argument or position. | |
| The above are the available grading measures. | |
| """Now, you need to pick/generate the suitable subset of above grading measures for the given question.""" | |
| {{~/user}} | |
| {{#assistant~}} | |
| {{gen 'grading_measures' temperature=0.7 max_tokens=100}} | |
| {{~/assistant}} | |
| {{#user~}} | |
| Consider the following inputs, to perform the task. | |
| The answer he had given to the questions is as follows : | |
| """{{answer}}""" | |
| if the above ans is empty then evaluate to zero in all blocks of grading measures | |
| The candidate's answer is to be evaluated as per below Conceptual Essay : | |
| {{essay}} | |
| {{~/user}} | |
| {{#user~}} | |
| Now consider the pattern of scoring as follows : | |
| """The evaluation score is awared 5 when the answer contains important information of the conceptual is found in it. | |
| The evaluation score is awared 4 when the answer has the important information but with minor omissions, inaccuracy, vagueness, imprecision and language errors. | |
| The evaluation score is awared 3 when the answer conveys only a global, unclear and inaccurate information. It is also done when key points | |
| of the conceptual essay are not found in the interviewee's answer. Frequent Grammatical Usage Errors are also seen. | |
| The evaluation score is given 2 when the answer is relevant to the conceptual essay but has major omission, inaccuracy and language difficulties. | |
| The evaluation score is presented with 1 if the answer has little to no meaning or relevant information from the conceptual essay. The language sophistication is too low to comphrehend. | |
| The evaluation score is determined to be 0 if the answer is a copy from the conceptual essay or has no relevance to the question or written in Non-English Language or is left as blank.""" | |
| {{~/user}} | |
| {{#user~}} | |
| Consider the following example of grading. | |
| The answer provided : | |
| And lastly, paragraph says that everyone feels responsible for their own part, and all together they | |
| are all more effective as a team. In the lecture, doubt was expressed concerning the advantages of | |
| the recent trend of forming teams to tackle projects, which was mentioned in the reading. | |
| to begin with, the lecturer agrues that although a group tends to have a greater resource of skills | |
| and expertises, these resources may not necessarily be effefectively used. according to a recent | |
| company project, it was found that one or two members dominated over the whole group, when | |
| the dominant members asserted or banned an idea, most of the other group members would | |
| follow their ideas and “suppress” the other ideas that were suggested, even if the other ideas were | |
| more creative and innovative. | |
| secondly, it was proved that, on the contrary of the reading, progress in the project was very slow. | |
| this was the result of long debates over reaching a compromise as ideas were diverted and | |
| consenus took a lenghty period of time. | |
| thirdly, as a group would be credited collectively, quite a number of unfair situations appeared. in | |
| the group. it was found that some members did not work hard at all and got a “free ride”. | |
| however, those worked harder were not rewarded for their extra efforts as their individual efforts | |
| would not be recognized. | |
| concluding, via the results of a recent company that adopted the “group method” of tackling | |
| projects, the lecturer projected doubts that contradicted with the central standpoint of the reading. | |
| the lecturer believes that skills and expertise cannot be maximized in a group, progress is slow | |
| and the overall results of the team is not a fair assessment of the individual members of the group- | |
| which contradicts with the central standpoint of the reading. | |
| Grade Evaluated : 5 | |
| {{~/user}} | |
| {{#user~}} | |
| Now, based on the given values of question, answer and the Conceptual Essay, | |
| perform the Grading Evaluation on the Answer text based on generated grading Measures. | |
| The answer evaluation must totally depend on the conceptual essay and pattern of scoring provided above. | |
| {{~/user}} | |
| {{#user~}} | |
| Generate the Evaulation in a JSON Format. The JSON must consist of Grading Measure as the key and a dictionary of two key-value pairs. | |
| The dict must have the first pair with key as 'numerical_score' and value as grade out of 5. The other element must have 'reason' as the key and value as a string containing reason to why setting such grade. | |
| The final JSON must also a Key Value of 'Overall Score' and dictionary as value with 'numerical_score' key having the mean value of all grading measure numerical scores. | |
| {{~/user}} | |
| {{#assistant~}} | |
| {{gen 'evaluation' temperature=0.5 max_tokens=1000}} | |
| {{~/assistant}} | |
| ''', llm=gptThreeFive) | |
| text = mySys(question= question, answer = answer, essay = essay) | |
| text=text['evaluation'] | |
| start_index = text.find('{') | |
| end_index = text.rfind('}') + 1 | |
| json_part = text[start_index:end_index] | |
| data = json.loads(json_part) | |
| # final = json.loads(output['evaluation']) | |
| # for i in final.keys(): | |
| # print("The Rubric : ",i,"\n\tThe Numerical Score Assigned : ", final[i]['numerical_score'],"\n\tThe Justification : ", final[i]['reason']) | |
| return data | |
| def k(question, answer, essay): | |
| if question=='' or answer=='' or essay=='': | |
| return {'error':'fill all the required fields'} | |
| # l=top_10_ans(question) | |
| return guideus(question,answer,essay) | |
| # css = """ | |
| # body { | |
| # background-color: #F2F2F2; | |
| # font-family: Arial, sans-serif; | |
| # } | |
| # .container { | |
| # padding: 20px; | |
| # border: 1px solid #DDD; | |
| # border-radius: 5px; | |
| # background-color: #FFFFFF; | |
| # } | |
| # h1 { | |
| # font-size: 24px; | |
| # color: #333333; | |
| # margin-bottom: 20px; | |
| # } | |
| # .input-container { | |
| # margin-bottom: 20px; | |
| # } | |
| # input[type="text"] { | |
| # width: 100%; | |
| # padding: 10px; | |
| # border: 1px solid #CCCCCC; | |
| # border-radius: 5px; | |
| # } | |
| # .button-container { | |
| # text-align: center; | |
| # } | |
| # button { | |
| # background-color: #4CAF50; | |
| # color: white; | |
| # padding: 10px 20px; | |
| # border: none; | |
| # border-radius: 5px; | |
| # cursor: pointer; | |
| # } | |
| # button:hover { | |
| # background-color: #45a049; | |
| # } | |
| # .output-container { | |
| # margin-top: 20px; | |
| # } | |
| # .result { | |
| # background-color: #E9E9E9; | |
| # padding: 10px; | |
| # border-radius: 5px; | |
| # } | |
| # """ | |
| textbox = gr.inputs.Textbox(label="Question") | |
| textbox1 = gr.inputs.Textbox(label="Answer") | |
| textbox2 = gr.inputs.Textbox(label="Essay") | |
| inputs = [ | |
| textbox,textbox1,textbox2 | |
| ] | |
| gr.Interface(fn=k, inputs=inputs, outputs=['json']).launch() | |