|
import streamlit as st |
|
import os |
|
import requests |
|
from groq import Groq |
|
import json |
|
from streamlit.components.v1 import html |
|
|
|
|
|
os.environ["GOOGLE_TRANSLATE_API_KEY"] = "AIzaSyB-4Nk7iTq4cyyTz3z6k7k-_HKKhWJF90g" |
|
os.environ["GROQ_API_KEY"] = "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" |
|
|
|
|
|
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") |
|
|
|
st.markdown(""" |
|
<style> |
|
.main { |
|
background-color: #f0f2f6; |
|
padding: 2rem; |
|
border-radius: 10px; |
|
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); |
|
} |
|
.title { |
|
color: #1e3a8a; |
|
font-size: 2.5rem; |
|
margin-bottom: 1rem; |
|
} |
|
.subtitle { |
|
color: #4a5568; |
|
font-size: 1.2rem; |
|
margin-bottom: 2rem; |
|
} |
|
.input-box { |
|
background-color: white; |
|
border: 1px solid #e2e8f0; |
|
border-radius: 5px; |
|
padding: 1rem; |
|
} |
|
.result-box { |
|
background-color: white; |
|
border: 1px solid #e2e8f0; |
|
border-radius: 5px; |
|
padding: 1rem; |
|
margin-top: 2rem; |
|
} |
|
</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"): |
|
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) |
|
|
|
|
|
st.balloons() |
|
|
|
|
|
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() |