Spaces:
Sleeping
Sleeping
from openai import AzureOpenAI | |
import docx2txt | |
import PyPDF2 | |
import os | |
import pdfplumber | |
from pdfminer.high_level import extract_text | |
class OpenaiAPI: | |
def __init__(self) -> None: | |
self.client = AzureOpenAI( | |
api_key=os.getenv("API"), | |
api_version="2023-07-01-preview", | |
azure_endpoint=os.getenv("URL"), | |
) | |
def get_response(self, prompt) -> str: | |
try: | |
completion = self.client.chat.completions.create( | |
model="GPT-4o", # e.g., gpt-35-instant | |
messages=prompt, | |
temperature=0, | |
) | |
return completion.choices[0].message.content | |
except Exception as e: | |
print(f"An error occurred while generating prompt from OpenAI API: {e}") | |
def docx_to_text(self, docx_path): | |
text = docx2txt.process(docx_path) | |
return text | |
def pdf_to_text_pypdf2(self, pdf_file): | |
text = extract_text(pdf_file) | |
return text | |
def save_uploadedfile(self, uploadedfile): | |
with open(os.path.join("tempDir", uploadedfile), "wb") as f: | |
f.write(uploadedfile.getbuffer()) | |
return st.success(f"Saved File: {uploadedfile.name} to tempDir") | |