File size: 3,420 Bytes
51b2aac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from api import Model
import os
def initial_prompt(list_of_articles, POSITIVE, NEGATIVE,cleaned_text):
    return f"""

You are a senior data analyst with 15+ years of expertise in comparative analysis. Your task is to analyze multiple articles from the given list and generate a structured **JSON summary** that highlights key differences, patterns, and sentiment insights.



### **Input Data:**  

- **Articles:** {list_of_articles} 





### Complete contents of the articles: 

{cleaned_text} 



### **Task Requirements:**  

1. **Identify Key Differences:**  

   - Highlight the **strongest contrasting viewpoints** or **highest topic overlap** among the articles.  



2. **Generate a Structured JSON Output:**  

   - **Coverage Differences:** Identify variations in focus across articles.  

   - **Impact Analysis:** Explain how these differences affect audience perception.  



3. **Perform Sentiment Analysis:**  

   - Generate a summary based on the sentiment distribution.  





### **Sentiment Overview:**  

- **Positive Mentions:** {POSITIVE}  

- **Negative Mentions:** {NEGATIVE}  

 

"""

prompt1 = """

### **Expected JSON Output Format:**  

{  

  "Coverage Differences": [  

    {  

      "Comparison": "{Article_X} focuses on {Key_Topic_X}, while {Article_Y} highlights {Key_Topic_Y}.",  

      "Impact": "{Effect_of_the_different_focuses_on_audience_perception}."  

    },  

    {  

      "Comparison": "{Article_X} emphasizes {Aspect_X}, whereas {Article_Y} discusses {Aspect_Y}.",  

      "Impact": "{Potential_market_or_public_reaction}."  

    },  

    {  

      "Comparison": "{Article_X} presents {Perspective_X}, but {Article_Y} contrasts this with {Perspective_Y}.",  

      "Impact": "{Implications_of_the_conflicting_perspectives}."  

    }  

  ],  

  "Topic Overlap": {  

    "Common Topics": ["{Common_Topic_1}", "{Common_Topic_2}"],  

    "Unique Topics in {Article_X}": ["{Unique_Topic_X_1}", "{Unique_Topic_X_2}"],  

    "Unique Topics in {Article_Y}": ["{Unique_Topic_Y_1}", "{Unique_Topic_Y_2}"]  

  },  

  "Final Sentiment Analysis": "{Overall_sentiment_summary (Positive/Negative/Neutral) with a brief explanation}",

  "Overall_Sentiment_Ssummarizing_Report" :"Complete contents of the articles content summarizing into give clean format." 

}  



### **Instructions:**  

- **Strictly** follow this JSON structure in your output.  

- Do **not** include any additional text besides the JSON.  

"""



def generate_response(list_of_articles,POSITIVE,NEGATIVE,clean_text):
    prompt = initial_prompt(list_of_articles,POSITIVE,NEGATIVE,clean_text) + "\n\n"+ prompt1
    answer_text= Model.OPENAI_MODEL(prompt)
    start_index_square = answer_text.find('[')
    start_index_curly = answer_text.find('{')
    if start_index_square != -1 and (start_index_curly == -1 or start_index_square < start_index_curly):
        start_index = start_index_square
        end_char = ']'
    elif start_index_curly != -1 and (start_index_square == -1 or start_index_curly < start_index_square):
        start_index = start_index_curly
        end_char = '}'
    else:
        return ("Error: JSON data not found.")
        extracted_json = None
    end_index = answer_text.rfind(end_char)
    if start_index != -1 and end_index != -1:
        extracted_json = answer_text[start_index:end_index + 1]
        return (extracted_json)