Spaces:
Build error
Build error
| 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(""" | |
| <style> | |
| .main { | |
| padding: 2rem; | |
| border-radius: 10px; | |
| } | |
| .title { | |
| color: #FF9933; | |
| font-size: 2.5rem; | |
| margin-bottom: 1rem; | |
| text-shadow: 2px 2px 4px rgba(0,0,0,0.1); | |
| } | |
| .subtitle { | |
| color: #6C8A9E; | |
| font-size: 1.2rem; | |
| margin-bottom: 2rem; | |
| } | |
| .stTextInput > div > div > input { | |
| background-color: rgba(255, 255, 255, 0.1); | |
| color: #FFFFFF; | |
| } | |
| .stTextArea > div > div > textarea { | |
| background-color: rgba(255, 255, 255, 0.1); | |
| color: #FF9933; | |
| } | |
| .result-box { | |
| background-color: rgba(255, 255, 255, 0.1); | |
| border-radius: 5px; | |
| padding: 1rem; | |
| margin-top: 2rem; | |
| } | |
| .stAlert { | |
| background-color: rgba(255, 255, 255, 0.1); | |
| color: #FFFFFF; | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| st.markdown('<h1 class="title">Urdu Sentiment Analysis</h1>', unsafe_allow_html=True) | |
| st.markdown('<p class="subtitle">Enter Urdu text to analyze its sentiment</p>', 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('<div class="result-box">', unsafe_allow_html=True) | |
| st.subheader("Sentiment Analysis Result:") | |
| st.write(sentiment) | |
| st.markdown('</div>', 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('<div class="result-box">', unsafe_allow_html=True) | |
| st.subheader("Fun Fact about Urdu:") | |
| st.write(fun_facts[hash(urdu_text) % len(fun_facts)]) | |
| st.markdown('</div>', unsafe_allow_html=True) | |
| else: | |
| st.warning("Please enter some Urdu text to analyze.") | |
| if __name__ == "__main__": | |
| main() |