Zeamays3427 commited on
Commit
18769cb
·
verified ·
1 Parent(s): 050d8f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -10
app.py CHANGED
@@ -10,7 +10,7 @@ import pandas as pd
10
  from sklearn.feature_extraction.text import TfidfVectorizer
11
 
12
  # Set OpenAI API key from environment variables
13
- openai.api_key = os.getenv("OPENAI_API_KEY")
14
 
15
  # Load the tokenizer and the pretrained classification model
16
  tokenizer = AutoTokenizer.from_pretrained("hamzab/roberta-fake-news-classification")
@@ -109,19 +109,22 @@ def generate_suggestions(title, text, keywords):
109
  Title: {title}
110
  Text: {text}
111
  """
112
-
113
  try:
114
- # Call OpenAI API to generate suggestions using the GPT-4 model
115
- response = openai.Completion.create(
116
- engine="gpt-4-2024-08-06", # The GPT-4 model being used
117
- prompt=prompt, # The constructed prompt
118
- max_tokens=1000, # Maximum number of tokens for the response
119
- temperature=0.7, # Controls randomness in the generated text
 
 
 
120
  )
121
 
122
  # Extract and clean the suggestions from the API response
123
- suggestions = response.choices[0].text.strip()
124
-
125
  except Exception as e:
126
  # If there's an error, set a default error message and print the exception details for debugging
127
  suggestions = "Unable to generate suggestions at this time."
 
10
  from sklearn.feature_extraction.text import TfidfVectorizer
11
 
12
  # Set OpenAI API key from environment variables
13
+ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
14
 
15
  # Load the tokenizer and the pretrained classification model
16
  tokenizer = AutoTokenizer.from_pretrained("hamzab/roberta-fake-news-classification")
 
109
  Title: {title}
110
  Text: {text}
111
  """
112
+
113
  try:
114
+ # Call OpenAI's chat completion method using GPT-4 model
115
+ response = client.chat.completions.create(
116
+ model="gpt-4", # Using the GPT-4 model
117
+ messages=[
118
+ {"role": "system", "content": "You are a helpful assistant specialized in fact-checking."}, # System role definition
119
+ {"role": "user", "content": prompt} # User input (the constructed prompt)
120
+ ],
121
+ max_tokens=4000, # Set the maximum token limit to 4000
122
+ temperature=0.7, # Controls the randomness in the generated text
123
  )
124
 
125
  # Extract and clean the suggestions from the API response
126
+ suggestions = response.choices[0].message["content"].strip()
127
+
128
  except Exception as e:
129
  # If there's an error, set a default error message and print the exception details for debugging
130
  suggestions = "Unable to generate suggestions at this time."