Warisamm748 commited on
Commit
b74ced7
·
verified ·
1 Parent(s): a1e8780

Create api.py

Browse files
Files changed (1) hide show
  1. api.py +125 -0
api.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Importing important libraries
2
+ from fastapi import FastAPI, Query,HTTPException
3
+ from fastapi.responses import JSONResponse, FileResponse, StreamingResponse
4
+ from google.cloud import texttospeech
5
+ from google.oauth2.service_account import Credentials
6
+ from langchain.schema import HumanMessage
7
+ from langchain_groq import ChatGroq
8
+ import json
9
+ from dotenv import load_dotenv
10
+ import os
11
+
12
+ # Importing utility functions for processing news articles
13
+ from utils import (
14
+ extract_titles_and_summaries,
15
+ perform_sentiment_analysis,
16
+ extract_topics_with_hf,
17
+ compare_articles
18
+ )
19
+
20
+ load_dotenv() # Loading environment variables from .env file
21
+ GROQ_API_KEY = os.getenv('GROQ_API_KEY')
22
+ PRIVATE_KEY = os.getenv('PRIVATE_KEY').replace("\\n", "\n")
23
+ CLIENT_EMAIL = os.getenv('CLIENT_EMAIL')
24
+
25
+ app = FastAPI(title="Company Sentiment API", description="Get company news summaries with sentiment analysis")
26
+
27
+ llm=ChatGroq(api_key=GROQ_API_KEY, model="llama-3.1-8b-instant")
28
+
29
+ JSON_FILE_PATH = "final_summary.json"
30
+ AUDIO_FILE_PATH = "hindi_summary.mp3"
31
+
32
+ def get_tts_client(): # Function to create a Text-to-Speech client
33
+ # Setting up Google Cloud credentials
34
+ credentials = Credentials.from_service_account_info({
35
+ "type": "service_account",
36
+ "private_key": PRIVATE_KEY,
37
+ "client_email": CLIENT_EMAIL,
38
+ "token_uri": "https://oauth2.googleapis.com/token"
39
+ })
40
+ return texttospeech.TextToSpeechClient(credentials=credentials)
41
+
42
+ # Creating main function to create final summarized report
43
+ def generate_summary(company_name):
44
+ news_articles = extract_titles_and_summaries(company_name)
45
+ news_articles, sentiment_counts = perform_sentiment_analysis(news_articles)
46
+ news_articles = extract_topics_with_hf(news_articles)
47
+ final_summary = compare_articles(news_articles, sentiment_counts)
48
+ hindi_text = ""
49
+ if PRIVATE_KEY and CLIENT_EMAIL:
50
+ hindi_prompt = f"Just Translate this text into Hindi: {final_summary['Final Sentiment Analysis']}" # Creating a prompt for Hindi translation
51
+ hindi_response = llm.invoke([HumanMessage(content=hindi_prompt)]).content
52
+ hindi_text = hindi_response.strip() if hindi_response else "Translation not available."
53
+ if hindi_text:
54
+ print(f"Generated Hindi Text: {hindi_text}")
55
+ else:
56
+ print("Hindi Text not generated")
57
+
58
+ try:
59
+ client = get_tts_client() # Getting the Text-to-Speech client
60
+ input_text = texttospeech.SynthesisInput(text=hindi_text) # Creating TTS input from Hindi text
61
+ voice = texttospeech.VoiceSelectionParams(
62
+ language_code="hi-IN",
63
+ name="hi-IN-Chirp3-HD-Kore"
64
+ )
65
+ audio_config = texttospeech.AudioConfig(audio_encoding=texttospeech.AudioEncoding.MP3) # Configuring MP3 audio output
66
+ response = client.synthesize_speech(input=input_text, voice=voice, audio_config=audio_config) # Synthesizing speech from text
67
+ with open(AUDIO_FILE_PATH, "wb") as out: # Writing the audio content to a file
68
+ out.write(response.audio_content)
69
+ print(f"Audio content written to file: {AUDIO_FILE_PATH}")
70
+
71
+ except Exception as e:
72
+ print(f"Error generating audio: {e}")
73
+ if not os.path.exists(AUDIO_FILE_PATH):
74
+ print(f"Audio file could not be found at {AUDIO_FILE_PATH}.")
75
+
76
+ final_summary["Audio"] = AUDIO_FILE_PATH
77
+
78
+ with open(JSON_FILE_PATH,"w",encoding="utf-8") as f:
79
+ json.dump(final_summary,f,ensure_ascii=False, indent=4)
80
+
81
+ # Returning a structured summary response
82
+ return {
83
+ 'Company': final_summary["Company"],
84
+ 'Articles': [
85
+ {
86
+ 'Title': article.get('Title', 'No Title'),
87
+ 'Summary': article.get('Summary', 'No Summary'),
88
+ 'Sentiment': article.get('Sentiment', 'Unknown'),
89
+ 'Score': article.get('Score', 0.0),
90
+ 'Topics': article.get('Topics', [])
91
+ }
92
+ for article in final_summary["Articles"]
93
+ ],
94
+ 'Comparative Sentiment Score': { # Structuring sentiment analysis comparison
95
+ 'Sentiment Distribution': sentiment_counts,
96
+ 'Coverage Differences': final_summary["Comparative Sentiment Score"].get("Coverage Differences", []),
97
+ 'Topic Overlap': {
98
+ 'Common Topics': final_summary["Comparative Sentiment Score"].get("Topic Overlap", {}).get("Common Topics", []),
99
+ 'Unique Topics': final_summary["Comparative Sentiment Score"].get("Topic Overlap", {}).get("Unique Topics", {})
100
+ }
101
+ },
102
+ 'Final Sentiment Analysis': final_summary["Final Sentiment Analysis"],
103
+ 'Audio': AUDIO_FILE_PATH
104
+ }
105
+
106
+ @app.get("/") # Defining a GET route for the home endpoint
107
+ def home():
108
+ return {"message": "Welcome to the Company Sentiment API"}
109
+
110
+ @app.post("/generateSummary") # Defining a POST route to generate a summary
111
+ def get_summary(company_name: str = Query(..., description="Enter company name")):
112
+ structured_summary = generate_summary(company_name)
113
+ return structured_summary
114
+
115
+ @app.get("/downloadJson") # Defining a GET route to download the JSON summary
116
+ def download_json():
117
+ return FileResponse(JSON_FILE_PATH, media_type="application/json", filename="final_summary.json")
118
+
119
+ @app.get("/downloadHindiAudio") # Defining a GET route to download Hindi audio
120
+ def download_audio():
121
+ return FileResponse(AUDIO_FILE_PATH, media_type="audio/mp3", filename="hindi_summary.mp3")
122
+
123
+ if __name__ == "__main__": # Main execution block for running the app
124
+ import uvicorn
125
+ uvicorn.run(app, host="0.0.0.0", port=8000)