Spaces:
Running
Running
Zeamays3427
commited on
Update app.py
Browse files
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 |
-
|
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
|
115 |
-
response =
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
|
|
|
|
|
|
120 |
)
|
121 |
|
122 |
# Extract and clean the suggestions from the API response
|
123 |
-
suggestions = response.choices[0].
|
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."
|