import streamlit as st import os import requests from groq import Groq import json from streamlit.components.v1 import html # Set up API keys os.environ["GOOGLE_TRANSLATE_API_KEY"] = "AIzaSyB-4Nk7iTq4cyyTz3z6k7k-_HKKhWJF90g" os.environ["GROQ_API_KEY"] = "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" # Initialize Groq client try: groq_client = Groq(api_key=os.environ["GROQ_API_KEY"]) except Exception as e: st.error(f"Error initializing Groq client: {e}") st.stop() def analyze_sentiment(text): try: prompt = f"""Analyze the sentiment of the following text and classify it as positive, negative, or neutral. Provide a brief explanation for your classification. Text: "{text}" Sentiment:""" chat_completion = groq_client.chat.completions.create( messages=[ { "role": "user", "content": prompt, } ], model="llama3-8b-8192", ) return chat_completion.choices[0].message.content except Exception as e: st.error(f"Error in sentiment analysis: {e}") return None def main(): st.set_page_config(page_title="Urdu Sentiment Analysis", page_icon="🇵🇰", layout="wide") # Custom CSS with improved color scheme st.markdown(""" """, unsafe_allow_html=True) st.markdown('

Urdu Sentiment Analysis

', unsafe_allow_html=True) st.markdown('

Enter Urdu text to analyze its sentiment

', unsafe_allow_html=True) urdu_text = st.text_area("Enter Urdu text:", key="input", height=150) if st.button("Analyze Sentiment", key="analyze_button"): if urdu_text: with st.spinner("Analyzing sentiment..."): sentiment = analyze_sentiment(urdu_text) if sentiment: st.markdown('
', unsafe_allow_html=True) st.subheader("Sentiment Analysis Result:") st.write(sentiment) st.markdown('
', unsafe_allow_html=True) # Display congratulation balloons st.balloons() # Add a fun fact about Urdu fun_facts = [ "Urdu is the national language of Pakistan and is also widely spoken in India.", "Urdu is written in the Perso-Arabic script and is read from right to left.", "The word 'Urdu' itself means 'camp' in Turkish.", "Urdu poetry, known as 'Shayari', is famous for its beauty and depth.", "Urdu has borrowed words from Arabic, Persian, Turkish, and even English." ] st.markdown('
', unsafe_allow_html=True) st.subheader("Fun Fact about Urdu:") st.write(fun_facts[hash(urdu_text) % len(fun_facts)]) st.markdown('
', unsafe_allow_html=True) else: st.warning("Please enter some Urdu text to analyze.") if __name__ == "__main__": main()