Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
+
from groq import Groq
|
| 5 |
+
import json
|
| 6 |
+
from streamlit.components.v1 import html
|
| 7 |
+
|
| 8 |
+
# Set up API keys
|
| 9 |
+
os.environ["GOOGLE_TRANSLATE_API_KEY"] = "AIzaSyB-4Nk7iTq4cyyTz3z6k7k-_HKKhWJF90g"
|
| 10 |
+
os.environ["GROQ_API_KEY"] = "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"
|
| 11 |
+
|
| 12 |
+
# Initialize Groq client
|
| 13 |
+
try:
|
| 14 |
+
groq_client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 15 |
+
except Exception as e:
|
| 16 |
+
st.error(f"Error initializing Groq client: {e}")
|
| 17 |
+
st.stop()
|
| 18 |
+
|
| 19 |
+
def analyze_sentiment(text):
|
| 20 |
+
try:
|
| 21 |
+
prompt = f"""Analyze the sentiment of the following text and classify it as positive, negative, or neutral. Provide a brief explanation for your classification.
|
| 22 |
+
Text: "{text}"
|
| 23 |
+
Sentiment:"""
|
| 24 |
+
chat_completion = groq_client.chat.completions.create(
|
| 25 |
+
messages=[
|
| 26 |
+
{
|
| 27 |
+
"role": "user",
|
| 28 |
+
"content": prompt,
|
| 29 |
+
}
|
| 30 |
+
],
|
| 31 |
+
model="llama3-8b-8192",
|
| 32 |
+
)
|
| 33 |
+
return chat_completion.choices[0].message.content
|
| 34 |
+
except Exception as e:
|
| 35 |
+
st.error(f"Error in sentiment analysis: {e}")
|
| 36 |
+
return None
|
| 37 |
+
|
| 38 |
+
def main():
|
| 39 |
+
st.set_page_config(page_title="Urdu Sentiment Analysis", page_icon="🇵🇰", layout="wide")
|
| 40 |
+
|
| 41 |
+
st.markdown("""
|
| 42 |
+
<style>
|
| 43 |
+
.main {
|
| 44 |
+
background-color: #f0f2f6;
|
| 45 |
+
padding: 2rem;
|
| 46 |
+
border-radius: 10px;
|
| 47 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 48 |
+
}
|
| 49 |
+
.title {
|
| 50 |
+
color: #1e3a8a;
|
| 51 |
+
font-size: 2.5rem;
|
| 52 |
+
margin-bottom: 1rem;
|
| 53 |
+
}
|
| 54 |
+
.subtitle {
|
| 55 |
+
color: #4a5568;
|
| 56 |
+
font-size: 1.2rem;
|
| 57 |
+
margin-bottom: 2rem;
|
| 58 |
+
}
|
| 59 |
+
.input-box {
|
| 60 |
+
background-color: white;
|
| 61 |
+
border: 1px solid #e2e8f0;
|
| 62 |
+
border-radius: 5px;
|
| 63 |
+
padding: 1rem;
|
| 64 |
+
}
|
| 65 |
+
.result-box {
|
| 66 |
+
background-color: white;
|
| 67 |
+
border: 1px solid #e2e8f0;
|
| 68 |
+
border-radius: 5px;
|
| 69 |
+
padding: 1rem;
|
| 70 |
+
margin-top: 2rem;
|
| 71 |
+
}
|
| 72 |
+
</style>
|
| 73 |
+
""", unsafe_allow_html=True)
|
| 74 |
+
|
| 75 |
+
st.markdown('<h1 class="title">Urdu Sentiment Analysis</h1>', unsafe_allow_html=True)
|
| 76 |
+
st.markdown('<p class="subtitle">Enter Urdu text to analyze its sentiment</p>', unsafe_allow_html=True)
|
| 77 |
+
|
| 78 |
+
urdu_text = st.text_area("Enter Urdu text:", key="input", height=150)
|
| 79 |
+
|
| 80 |
+
if st.button("Analyze Sentiment"):
|
| 81 |
+
if urdu_text:
|
| 82 |
+
with st.spinner("Analyzing sentiment..."):
|
| 83 |
+
sentiment = analyze_sentiment(urdu_text)
|
| 84 |
+
|
| 85 |
+
if sentiment:
|
| 86 |
+
st.markdown('<div class="result-box">', unsafe_allow_html=True)
|
| 87 |
+
st.subheader("Sentiment Analysis Result:")
|
| 88 |
+
st.write(sentiment)
|
| 89 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 90 |
+
|
| 91 |
+
# Display congratulation balloons
|
| 92 |
+
st.balloons()
|
| 93 |
+
|
| 94 |
+
# Add a fun fact about Urdu
|
| 95 |
+
fun_facts = [
|
| 96 |
+
"Urdu is the national language of Pakistan and is also widely spoken in India.",
|
| 97 |
+
"Urdu is written in the Perso-Arabic script and is read from right to left.",
|
| 98 |
+
"The word 'Urdu' itself means 'camp' in Turkish.",
|
| 99 |
+
"Urdu poetry, known as 'Shayari', is famous for its beauty and depth.",
|
| 100 |
+
"Urdu has borrowed words from Arabic, Persian, Turkish, and even English."
|
| 101 |
+
]
|
| 102 |
+
st.markdown('<div class="result-box">', unsafe_allow_html=True)
|
| 103 |
+
st.subheader("Fun Fact about Urdu:")
|
| 104 |
+
st.write(fun_facts[hash(urdu_text) % len(fun_facts)])
|
| 105 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 106 |
+
else:
|
| 107 |
+
st.warning("Please enter some Urdu text to analyze.")
|
| 108 |
+
|
| 109 |
+
if __name__ == "__main__":
|
| 110 |
+
main()
|