Skip to content

Commit 910e6ba

Browse files
committed
Update test.py
1 parent a84662e commit 910e6ba

File tree

1 file changed

+27
-37
lines changed

1 file changed

+27
-37
lines changed

Backend/test.py

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,29 @@
11
import openai
2+
import shutil
3+
import os
4+
from fastapi import FastAPI,UploadFile,File
5+
from fastapi import APIRouter
26

3-
# Set up your OpenAI API credentials
4-
openai.api_key = ""
5-
6-
7-
# Define your input parameters
8-
with open('#define path sorted questions here', 'r') as file:
9-
sorted_questions = str(file.read())
10-
with open('#define path notebook questions here', 'r') as file:
11-
notebook_questions = str(file.read())
12-
with open('#define path for important topics here', 'r') as file:
13-
important_topics = str(file.read())
14-
with open('#define path for reference paper here', 'r', encoding='latin-1') as file:
15-
reference_paper = str(file.read())
16-
17-
18-
# Construct the prompt for question generation
19-
prompt = f"generate 10 questions that are not in reference paper but similar \nSorted Previous Year Questions:\n{sorted_questions}\n\nNotebook Question List:\n{notebook_questions}\n\nImportant Topics:\n{important_topics}\n\nReference Paper:\n{reference_paper}\n\n"
20-
21-
22-
# Generate questions using OpenAI API
23-
response = openai.Completion.create(
24-
engine="davinci",
25-
prompt=prompt,
26-
max_tokens=1000, # Adjust the max_tokens value as per your requirement
27-
temperature=0.4, # Adjust the temperature value to control the creativity of the generated questions
28-
top_p=1.0,
29-
frequency_penalty=0.0,
30-
presence_penalty=0.0,
31-
n=5 # Adjust the 'n' value to generate more or fewer questions
32-
)
33-
34-
# Extract the generated questions from the API response
35-
generated_questions = [choice['text'].strip() for choice in response.choices]
36-
37-
# Print the generated questions
38-
for i, question in enumerate(generated_questions):
39-
print(f"Question {i+1}: {question}")
7+
def temp_file(file):
8+
try:
9+
with open("dat.txt", "wb") as buffer: # saving file
10+
shutil.copyfileobj(file.file, buffer)
11+
finally:
12+
file.file.close()
13+
14+
with open("temp.txt", "r", encoding='utf-8') as file:
15+
text = file.read()
16+
17+
print("Deleting the saved file.......")
18+
os.remove("temp.txt")
19+
print("deleted....")
20+
return text
21+
22+
23+
router_generate_question = APIRouter()
24+
25+
26+
27+
def gen_questions(file: UploadFile = File(...)):
28+
text = temp_file(file)
29+
return text

0 commit comments

Comments
 (0)