Divyansh Kushwaha
		
	commited on
		
		
					Commit 
							
							·
						
						657a977
	
1
								Parent(s):
							
							d97cb07
								
APIs exposing file updated
Browse files
    	
        api.py
    CHANGED
    
    | 
         @@ -1,119 +1,127 @@ 
     | 
|
| 1 | 
         
            -
             
     | 
| 2 | 
         
            -
            from fastapi 
     | 
| 3 | 
         
            -
            from  
     | 
| 4 | 
         
            -
            from google. 
     | 
| 5 | 
         
            -
            from  
     | 
| 6 | 
         
            -
            from  
     | 
| 7 | 
         
            -
            import  
     | 
| 8 | 
         
            -
             
     | 
| 9 | 
         
            -
            import  
     | 
| 10 | 
         
            -
             
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 11 | 
         
             
                extract_titles_and_summaries,
         
     | 
| 12 | 
         
             
                perform_sentiment_analysis,
         
     | 
| 13 | 
         
             
                extract_topics_with_hf,
         
     | 
| 14 | 
         
             
                compare_articles
         
     | 
| 15 | 
         
             
            )
         
     | 
| 16 | 
         | 
| 17 | 
         
            -
            load_dotenv()
         
     | 
| 18 | 
         
            -
            GROQ_API_KEY = os.getenv('GROQ_API_KEY')
         
     | 
| 19 | 
         
            -
            PRIVATE_KEY = os.getenv('PRIVATE_KEY').replace("\\n", "\n")
         
     | 
| 20 | 
         
            -
            CLIENT_EMAIL = os.getenv('CLIENT_EMAIL')
         
     | 
| 21 | 
         | 
| 22 | 
         
            -
            app = FastAPI(title="Company Sentiment API", description="Get company news summaries with sentiment analysis")
         
     | 
| 23 | 
         | 
| 24 | 
         
            -
            llm=ChatGroq(api_key=GROQ_API_KEY, model="llama-3.1-8b-instant")
         
     | 
| 25 | 
         | 
| 26 | 
         
            -
            JSON_FILE_PATH = "final_summary.json"
         
     | 
| 27 | 
         
            -
            AUDIO_FILE_PATH = "hindi_summary.mp3"
         
     | 
| 28 | 
         | 
| 29 | 
         
            -
            def get_tts_client():
         
     | 
| 30 | 
         
            -
                 
     | 
| 
         | 
|
| 31 | 
         
             
                    "type": "service_account",
         
     | 
| 32 | 
         
             
                    "private_key": PRIVATE_KEY,
         
     | 
| 33 | 
         
             
                    "client_email": CLIENT_EMAIL,
         
     | 
| 34 | 
         
             
                    "token_uri": "https://oauth2.googleapis.com/token"
         
     | 
| 35 | 
         
             
                })
         
     | 
| 36 | 
         
            -
                return texttospeech.TextToSpeechClient(credentials=credentials)
         
     | 
| 37 | 
         
            -
             
     | 
| 38 | 
         
            -
             
     | 
| 39 | 
         
            -
             
     | 
| 40 | 
         
            -
                news_articles 
     | 
| 41 | 
         
            -
                news_articles =  
     | 
| 42 | 
         
            -
                 
     | 
| 43 | 
         
            -
                 
     | 
| 44 | 
         
            -
                 
     | 
| 45 | 
         
            -
             
     | 
| 46 | 
         
            -
                     
     | 
| 47 | 
         
            -
                     
     | 
| 48 | 
         
            -
                    if  
     | 
| 49 | 
         
            -
             
     | 
| 
         | 
|
| 50 | 
         
             
                    else:
         
     | 
| 51 | 
         
            -
                        print("Hindi Text not generated")
         
     | 
| 52 | 
         | 
| 53 | 
         
             
                    try:
         
     | 
| 54 | 
         
            -
                        client = get_tts_client()
         
     | 
| 55 | 
         
            -
                        input_text = texttospeech.SynthesisInput(text=hindi_text)
         
     | 
| 56 | 
         
            -
                        voice = texttospeech.VoiceSelectionParams(
         
     | 
| 57 | 
         
             
                            language_code="hi-IN",
         
     | 
| 58 | 
         
             
                            name="hi-IN-Chirp3-HD-Kore"
         
     | 
| 59 | 
         
             
                        )
         
     | 
| 60 | 
         
            -
                        audio_config = texttospeech.AudioConfig(audio_encoding=texttospeech.AudioEncoding.MP3)
         
     | 
| 61 | 
         
            -
                        response = client.synthesize_speech(input=input_text, voice=voice, audio_config=audio_config)
         
     | 
| 62 | 
         
            -
                        with open(AUDIO_FILE_PATH, "wb") as out:
         
     | 
| 63 | 
         
             
                            out.write(response.audio_content)
         
     | 
| 64 | 
         
            -
                            print(f"Audio content written to file: {AUDIO_FILE_PATH}")
         
     | 
| 65 | 
         | 
| 66 | 
         
            -
                    except Exception as e:
         
     | 
| 67 | 
         
            -
                        print(f"Error generating audio: {e}")
         
     | 
| 68 | 
         
            -
                    if not os.path.exists(AUDIO_FILE_PATH):
         
     | 
| 69 | 
         
            -
                        print(f"Audio file could not be found at {AUDIO_FILE_PATH}.")
         
     | 
| 70 | 
         | 
| 71 | 
         
            -
                final_summary["Audio"] = AUDIO_FILE_PATH
         
     | 
| 72 | 
         | 
| 73 | 
         
            -
                with open(JSON_FILE_PATH,"w",encoding="utf-8") as f:
         
     | 
| 74 | 
         
            -
                    json.dump(final_summary,f,ensure_ascii=False, indent=4)
         
     | 
| 75 | 
         
            -
             
     | 
| 76 | 
         
            -
                 
     | 
| 
         | 
|
| 77 | 
         
             
                    'Company': final_summary["Company"],
         
     | 
| 78 | 
         
             
                    'Articles': [
         
     | 
| 79 | 
         
             
                        {
         
     | 
| 80 | 
         
            -
                            'Title': article.get('Title', 'No Title'),
         
     | 
| 81 | 
         
            -
                            'Summary': article.get('Summary', 'No Summary'),
         
     | 
| 82 | 
         
            -
                            'Sentiment': article.get('Sentiment', 'Unknown'),
         
     | 
| 83 | 
         
            -
                            'Score': article.get('Score', 0.0),
         
     | 
| 84 | 
         
            -
                            'Topics': article.get('Topics', [])
         
     | 
| 85 | 
         
             
                        }
         
     | 
| 86 | 
         
            -
                        for article in final_summary["Articles"]
         
     | 
| 87 | 
         
             
                    ],
         
     | 
| 88 | 
         
            -
                    'Comparative Sentiment Score': {
         
     | 
| 89 | 
         
            -
                        'Sentiment Distribution': sentiment_counts,
         
     | 
| 90 | 
         
            -
                        'Coverage Differences': final_summary["Comparative Sentiment Score"].get("Coverage Differences", []),
         
     | 
| 91 | 
         
            -
                        'Topic Overlap': {
         
     | 
| 92 | 
         
             
                            'Common Topics': final_summary["Comparative Sentiment Score"].get("Topic Overlap", {}).get("Common Topics", []),
         
     | 
| 93 | 
         
             
                            'Unique Topics': final_summary["Comparative Sentiment Score"].get("Topic Overlap", {}).get("Unique Topics", {})
         
     | 
| 94 | 
         
             
                        }
         
     | 
| 95 | 
         
             
                    },
         
     | 
| 96 | 
         
            -
                    'Final Sentiment Analysis': final_summary["Final Sentiment Analysis"],
         
     | 
| 97 | 
         
            -
                    'Audio': AUDIO_FILE_PATH
         
     | 
| 98 | 
         
             
                }
         
     | 
| 99 | 
         | 
| 100 | 
         
            -
            @app.get("/")
         
     | 
| 101 | 
         
             
            def home():
         
     | 
| 102 | 
         
            -
                return {"message": "Welcome to the Company Sentiment API"}
         
     | 
| 103 | 
         | 
| 104 | 
         
            -
            @app.post("/generateSummary")
         
     | 
| 105 | 
         
            -
            def get_summary(company_name: str = Query(..., description="Enter company name")):
         
     | 
| 106 | 
         
            -
                structured_summary = generate_summary(company_name)
         
     | 
| 107 | 
         
            -
                return structured_summary
         
     | 
| 108 | 
         | 
| 109 | 
         
            -
            @app.get("/downloadJson")
         
     | 
| 110 | 
         
             
            def download_json():
         
     | 
| 111 | 
         
            -
                return FileResponse(JSON_FILE_PATH, media_type="application/json", filename="final_summary.json")
         
     | 
| 112 | 
         | 
| 113 | 
         
            -
            @app.get("/downloadHindiAudio")
         
     | 
| 114 | 
         
             
            def download_audio():
         
     | 
| 115 | 
         
            -
                return FileResponse(AUDIO_FILE_PATH, media_type="audio/mp3", filename="hindi_summary.mp3")
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 116 | 
         | 
| 117 | 
         
            -
            if __name__ == "__main__":
         
     | 
| 118 | 
         
            -
                import uvicorn
         
     | 
| 119 | 
         
            -
                uvicorn.run(app, host="0.0.0.0", port=8000)
         
     | 
| 
         | 
|
| 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)  
         
     | 
| 126 | 
         
            +
             
     | 
| 127 | 
         | 
| 
         | 
|
| 
         | 
|
| 
         |