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('
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('