Spaces:
Runtime error
Runtime error
import os | |
import openai | |
import PyPDF2 | |
import gradio as gr | |
import docx | |
from openai import AzureOpenAI | |
class CourseGenarator: | |
def __init__(self): | |
pass | |
def extract_text_from_file(self,file_path): | |
# Get the file extension | |
file_extension = os.path.splitext(file_path)[1] | |
if file_extension == '.pdf': | |
with open(file_path, 'rb') as file: | |
# Create a PDF file reader object | |
reader = PyPDF2.PdfFileReader(file) | |
# Create an empty string to hold the extracted text | |
extracted_text = "" | |
# Loop through each page in the PDF and extract the text | |
for page_number in range(reader.getNumPages()): | |
page = reader.getPage(page_number) | |
extracted_text += page.extractText() | |
return extracted_text | |
elif file_extension == '.txt': | |
with open(file_path, 'r') as file: | |
# Just read the entire contents of the text file | |
return file.read() | |
elif file_extension == '.docx': | |
doc = docx.Document(file_path) | |
text = [] | |
for paragraph in doc.paragraphs: | |
text.append(paragraph.text) | |
return '\n'.join(text) | |
else: | |
return "Unsupported file type" | |
def response(self,resume_path): | |
client = AzureOpenAI(api_key=os.getenv("AZURE_OPENAI_KEY"), | |
api_version="2023-07-01-preview", | |
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"), | |
azure_deployment = "GPT-3") | |
resume_path = resume_path.name | |
resume = self.extract_text_from_file(resume_path) | |
# Create a conversation for the OpenAI chat API | |
conversation = [ | |
{"role": "system", "content": "You are a Resume Summarizer."}, | |
{"role": "user", "content": f"""Analyze the Given Resume and Summarize {resume}"""} | |
] | |
# Call OpenAI GPT-3.5-turbo | |
chat_completion = client.chat.completions.create( | |
model = "ChatGPT", | |
messages = conversation, | |
max_tokens=500, | |
temperature=0 | |
) | |
generated_text = chat_completion.choices[0].message.content | |
return generated_text | |
def gradio_interface(self): | |
with gr.Blocks(css="style.css",theme="freddyaboulton/test-blue") as app: | |
gr.HTML(""" | |
<center><h1 class ="center" style="color:#fff">ADOPLE AI</h1></center> | |
<br><center><h1 style="color:#fff">Resume Summarizer</h1></center>""") | |
with gr.Row(elem_id="col-container"): | |
with gr.Column(): | |
resume = gr.File(label="Resume",elem_classes="heightfit") | |
with gr.Column(): | |
analyse = gr.Button("Analyze") | |
with gr.Column(): | |
result = gr.Textbox(label="Summarized",lines=8) | |
analyse.click(self.response, [resume], result) | |
print(result) | |
app.launch(debug=True) | |
ques = CourseGenarator() | |
ques.gradio_interface() |