Delete generate_explanation.py
Browse files- generate_explanation.py +0 -58
generate_explanation.py
DELETED
@@ -1,58 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import google.generativeai as genai
|
3 |
-
from dotenv import load_dotenv
|
4 |
-
|
5 |
-
# Load environment variables
|
6 |
-
load_dotenv()
|
7 |
-
|
8 |
-
# Configure Gemini API
|
9 |
-
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
10 |
-
|
11 |
-
# Model configuration
|
12 |
-
generation_config = {
|
13 |
-
"temperature": 0.7,
|
14 |
-
"top_p": 0.9,
|
15 |
-
"top_k": 40,
|
16 |
-
"max_output_tokens": 300,
|
17 |
-
"response_mime_type": "text/plain",
|
18 |
-
}
|
19 |
-
|
20 |
-
# Initialize the Gemini model
|
21 |
-
model = genai.GenerativeModel(
|
22 |
-
model_name="gemini-1.5-flash",
|
23 |
-
generation_config=generation_config,
|
24 |
-
)
|
25 |
-
|
26 |
-
|
27 |
-
def generate_explanation(news, context, truth_score):
|
28 |
-
"""
|
29 |
-
Generate a concise explanation supporting the truth score.
|
30 |
-
|
31 |
-
Arguments:
|
32 |
-
- news (str): The news or claim being analyzed.
|
33 |
-
- context (str): Relevant context from external sources.
|
34 |
-
- truth_score (float): The score indicating the truthfulness of the news.
|
35 |
-
|
36 |
-
Returns:
|
37 |
-
- str: A simple explanation or an empty string if an error occurs.
|
38 |
-
"""
|
39 |
-
|
40 |
-
# Define the prompt for explanation
|
41 |
-
prompt = (
|
42 |
-
"Summarize the context below and explain why the given truth score was assigned to the news claim.\n\n"
|
43 |
-
f"News: {news}\n\n"
|
44 |
-
f"Context: {context[:3000]}...\n\n"
|
45 |
-
f"Truth Score: {truth_score:.2f}\n\n"
|
46 |
-
"Keep the explanation short, factual, and easy to understand."
|
47 |
-
)
|
48 |
-
|
49 |
-
# Generate explanation
|
50 |
-
try:
|
51 |
-
chat_session = model.start_chat(history=[]) # Start a new chat session
|
52 |
-
response = chat_session.send_message(prompt)
|
53 |
-
return response.text.strip() # Return only the explanation text
|
54 |
-
|
55 |
-
except Exception as e:
|
56 |
-
# Print error and return an empty string
|
57 |
-
print(f"Error generating explanation: {str(e)}")
|
58 |
-
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|