Rafa1986 commited on
Commit
f501134
·
verified ·
1 Parent(s): 9e68bae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -6
app.py CHANGED
@@ -4,7 +4,6 @@ import PyPDF2
4
  import pandas as pd
5
  import openai
6
  import docx
7
- import requests
8
  import json
9
  from docx import Document
10
  from langchain_community.embeddings import OpenAIEmbeddings
@@ -14,7 +13,10 @@ from langchain.text_splitter import RecursiveCharacterTextSplitter
14
 
15
  def detect_language(text):
16
  """Detects the language of the input text using OpenAI."""
17
- client = openai.OpenAI()
 
 
 
18
  response = client.chat.completions.create(
19
  model="gpt-3.5-turbo",
20
  messages=[
@@ -24,9 +26,6 @@ def detect_language(text):
24
  )
25
  return response.choices[0].message.content.strip()
26
 
27
- # Set up OpenAI API key (replace with your key)
28
- openai.api_key = os.getenv("OPENAI_API_KEY")
29
-
30
  def extract_files_from_folder(folder_path):
31
  """Scans a folder and its subfolders for PDF, TXT, CSV, DOCX, and IPYNB files."""
32
  extracted_files = {"pdf": [], "txt": [], "csv": [], "docx": [], "ipynb": []}
@@ -110,7 +109,10 @@ def combine_text_from_files(extracted_files):
110
 
111
  def generate_response(question, text):
112
  """Uses OpenAI to answer a question based on extracted text."""
113
- client = openai.OpenAI()
 
 
 
114
  response = client.chat.completions.create(
115
  model="gpt-3.5-turbo",
116
  messages=[
 
4
  import pandas as pd
5
  import openai
6
  import docx
 
7
  import json
8
  from docx import Document
9
  from langchain_community.embeddings import OpenAIEmbeddings
 
13
 
14
  def detect_language(text):
15
  """Detects the language of the input text using OpenAI."""
16
+ api_key = os.getenv("OPENAI_API_KEY")
17
+ if not api_key:
18
+ raise ValueError("API Key da OpenAI não definida. Configure a variável de ambiente OPENAI_API_KEY.")
19
+ client = openai.Client(api_key=api_key)
20
  response = client.chat.completions.create(
21
  model="gpt-3.5-turbo",
22
  messages=[
 
26
  )
27
  return response.choices[0].message.content.strip()
28
 
 
 
 
29
  def extract_files_from_folder(folder_path):
30
  """Scans a folder and its subfolders for PDF, TXT, CSV, DOCX, and IPYNB files."""
31
  extracted_files = {"pdf": [], "txt": [], "csv": [], "docx": [], "ipynb": []}
 
109
 
110
  def generate_response(question, text):
111
  """Uses OpenAI to answer a question based on extracted text."""
112
+ api_key = os.getenv("OPENAI_API_KEY")
113
+ if not api_key:
114
+ raise ValueError("API Key da OpenAI não definida. Configure a variável de ambiente OPENAI_API_KEY.")
115
+ client = openai.Client(api_key=api_key)
116
  response = client.chat.completions.create(
117
  model="gpt-3.5-turbo",
118
  messages=[