File size: 4,933 Bytes
26ce7c2
4cad79d
26ce7c2
 
 
 
4cad79d
 
 
 
 
 
dd70a62
 
26ce7c2
4cad79d
 
 
 
 
 
 
 
 
 
 
 
26ce7c2
4cad79d
 
 
 
26ce7c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd70a62
 
4cad79d
 
 
 
dd70a62
 
4cad79d
dd70a62
 
 
 
 
26ce7c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import os
import requests
import streamlit as st
from googletrans import Translator
import asyncio

# Ensure you have the correct API URL for Groq and the key is set
GROQ_API_URL = "https://api.groq.com/v1/completions"  # Replace with the actual endpoint if different
API_KEY = os.environ.get("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")

if not API_KEY:
    raise ValueError("API key is missing. Make sure to set the GROQ_API_KEY environment variable.")

# Function to get recommendations from Groq API based on user input
def get_opportunities(user_query):
    headers = {
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json"
    }
    
    payload = {
        "model": "llama-3.3-70b-versatile",  # Example model, replace with the actual model you want
        "messages": [{"role": "user", "content": user_query}],
    }
    
    # Send the request to the Groq API
    response = requests.post(GROQ_API_URL, json=payload, headers=headers)
    
    if response.status_code == 200:
        return response.json()  # Adjust as per Groq's response format
    else:
        raise ValueError(f"Error fetching data from Groq API: {response.status_code}, {response.text}")

# Function to translate text into the selected language (async version)
async def translate_text(text, target_language):
    translator = Translator()
    translated = await translator.translate(text, dest=target_language)
    return translated.text

# Streamlit App Interface
st.set_page_config(page_title="AI-Powered Opportunity Finder", page_icon=":bulb:", layout="wide")
st.title("AI-Powered Opportunity Finder for Youth")

# Custom CSS for improving the UI
st.markdown("""
    <style>
        .css-1v0mbdj {padding-top: 30px; font-size: 1.5rem;}
        .css-1wa3m6h {padding: 10px; background-color: #f0f0f5;}
        .css-1w4t6pm {border: 1px solid #ccc; padding: 10px; background-color: #fff;}
        .css-1f4y1re {font-size: 1.2rem;}
    </style>
""", unsafe_allow_html=True)

# Sidebar for input fields
st.sidebar.header("Ask the AI Chatbot for Opportunities")

# Language selection
languages = {
    "English": "English",
    "Spanish": "Spanish",
    "French": "French",
    "German": "German",
    "Italian": "Italian",
    "Chinese": "Chinese",
    "Japanese": "Japanese",
    "Urdu": "Urdu"
}

selected_language = st.sidebar.selectbox("Select your preferred language:", list(languages.keys()))

# Chat history
if "messages" not in st.session_state:
    st.session_state.messages = []

# Display chat history
for message in st.session_state.messages:
    if message["role"] == "user":
        st.markdown(f"**You:** {message['content']}")
    else:
        st.markdown(f"**AI:** {message['content']}")

# Input box for user query
user_query = st.text_input("Ask me about scholarships, internships, or online courses:")

# Button to send message to chatbot
if st.button("Send"):
    if user_query:
        # Append user query to chat history
        st.session_state.messages.append({"role": "user", "content": user_query})
        
        with st.spinner("Fetching opportunities..."):
            try:
                # Get the opportunity details based on user query
                opportunities_response = get_opportunities(user_query)
                
                # Extract the response content, modify based on actual response format
                opportunities_text = opportunities_response.get("choices", [{}])[0].get("message", {}).get("content", "No data found.")
                
                # Run the async translate function and get the translated text
                translated_opportunities = asyncio.run(translate_text(opportunities_text, languages[selected_language]))
                
                # Append AI response to chat history
                st.session_state.messages.append({"role": "ai", "content": translated_opportunities})
            except Exception as e:
                st.error(f"Error: {e}")
        
        # Scroll to the latest message
        st.experimental_rerun()
    else:
        st.error("Please enter a query.")

# Add a footer with contact info and clickable links
st.markdown("""
    <footer style="text-align:center; padding: 20px; font-size: 1rem; background-color: #f0f0f5;">
        <p>Powered by Groq, Google Translate, and Streamlit</p>
        <p>For more opportunities, visit:</p>
        <ul style="list-style-type:none; padding: 0;">
            <li><a href="https://www.groq.com" target="_blank">Groq - AI Solutions</a></li>
            <li><a href="https://www.scholarships.com" target="_blank">Scholarships.com</a></li>
            <li><a href="https://www.coursera.org" target="_blank">Coursera - Online Courses</a></li>
            <li><a href="https://www.linkedin.com/jobs" target="_blank">LinkedIn Jobs</a></li>
        </ul>
        <p>Contact: [email protected]</p>
    </footer>
""", unsafe_allow_html=True)