Python Project: AI-Based Assignment Completer
- file 1:
- this file is to generate c files.
note - make sure replace API key with yours in line 7 or set the value of openai.api_key to your API key
secondly, write your questions in a text file named prompt.txt in the same directory where you have saved your programs and also create a folder named programms in the same directory
import osimport openai
class Ai: number=0 def automatedquery(self, query): '''this f(x) returns the responce generated by cahtgpt''' openai.api_key = "Your Api Key Here!" response = openai.Completion.create( model="text-davinci-003", prompt=query, temperature=0.7, max_tokens=256, top_p=1, frequency_penalty=0, presence_penalty=0 ) return(response["choices"][0]["text"]) #i in this f(x) is for checking for errors in filename def writeanswers(self, query, i=0): self.number+=1 with open(f'''programms//{self.number}.c''', "w") as fh: #responce from chatgpt result=self.automatedquery(f"{query} in c language without output and comments") #writing responce fh.write(f'''/*\t\tprogram {self.number}\n\t{query}\nRoll No - 2271268\nName- Rohit Singh\nCourse - BCA\nSemester-II\nSection-B\n*/\n{result}''') return 1class FileItemList: def __init__(self, file): self.list=[] with open (file) as fh: self.list=(fh.read()).split("\n")
if __name__ =="__main__": questions=FileItemList("prompt.txt") assistant=Ai() for index, query in enumerate(questions.list):
if assistant.writeanswers(query): print(f"{index+1} question written") else: print("something went wrong!")
2. File 2:
- this file is to generate output of those programs
from generate import Ai
import os
class Output:
fileExtention=".c"#because i am doing it for c program
def __init__(self, location):
self.location=location
if os.path.exists(self.location):
self.list_of_programs=[i for i in os.listdir(self.location) if i.endswith(self.fileExtention)]
else:
self.list_of_programs=[]
print(f"Location not found!")
#to get output of program
def getout(self, program):
solver=Ai()
output=solver.automatedquery(f'''generate the output of the following code if need of input give any- \n{program}''')
return output
#for all the files
def outputall(self):
for index,programms in enumerate(self.list_of_programs):
with open(fr"{self.location}{programms}") as fh:
program=fh.read()
output=self.getout(program)
with open (fr"{self.location}{programms}","a") as fh:
fh.write(f'''\n/*\n{output}\n*/''')
print(index+1, "output written")
return 1
if __name__=="__main__":
o=Output("/media/rohiyaa/link_between/c project/programms/")
o.outputall()
3. File 3:
this file is to copy code into a .docx file
import os
with open("file.docx", "a") as fh:
l=[i for i in os.listdir("programms/") if i.endswith(".c")]
#bubble sort
for i in range(len(l)-1):
for j in range(0, len(l)-i-1):
if int(l[j][0:-2])>int(l[j+1][0:-2]):
l[j], l[j+1]=l[j+1],l[j]
for i in l:
with open(f"programms/{i}") as f:
fh.write(f.read())
now you can print your .docx file make sure you are using Linux instead of .docx you can also use .txt format if the .docx is not opening in Windows OS.
I have this youtube channel called Road2geeks here you'll be seeing various tutorials along with some knowledge that I have voiceover so do also check our channel Road2geeks that's all for this blog friends thank you very much for reading this blog, Meet you in the next blog until then stay safe Jai hind
0 Comments
Share your views on this blog😍😍