saherPervaiz commited on
Commit
26ce7c2
·
verified ·
1 Parent(s): 7bde1c2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -0
app.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from groq import Groq
4
+ from googletrans import Translator
5
+ import asyncio
6
+
7
+ # Function to get recommendations from Groq AI based on user input
8
+ def get_opportunities(user_query):
9
+ # Fetch the API key from the environment variable
10
+ api_key = "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"
11
+
12
+ if not api_key:
13
+ raise ValueError("API key is missing. Make sure to set the GROQ_API_KEY environment variable.")
14
+
15
+ # Initialize the Groq client with the API key
16
+ client = Groq(api_key=api_key)
17
+
18
+ # Construct the query
19
+ query = f"Based on the user's query '{user_query}', find scholarships, internships, online courses, and career advice suitable for them, including deadlines for application and links to apply."
20
+
21
+ # Request to Groq API
22
+ response = client.chat.completions.create(
23
+ messages=[{"role": "user", "content": query}],
24
+ model="llama-3.3-70b-versatile",
25
+ )
26
+
27
+ return response.choices[0].message.content
28
+
29
+ # Function to translate text into the selected language (async version)
30
+ async def translate_text(text, target_language):
31
+ translator = Translator()
32
+ translated = await translator.translate(text, dest=target_language)
33
+ return translated.text
34
+
35
+ # Streamlit App Interface
36
+ st.set_page_config(page_title="AI-Powered Opportunity Finder", page_icon=":bulb:", layout="wide")
37
+ st.title("AI-Powered Opportunity Finder for Youth")
38
+
39
+ # Custom CSS for improving the UI
40
+ st.markdown("""
41
+ <style>
42
+ .css-1v0mbdj {padding-top: 30px; font-size: 1.5rem;}
43
+ .css-1wa3m6h {padding: 10px; background-color: #f0f0f5;}
44
+ .css-1w4t6pm {border: 1px solid #ccc; padding: 10px; background-color: #fff;}
45
+ .css-1f4y1re {font-size: 1.2rem;}
46
+ </style>
47
+ """, unsafe_allow_html=True)
48
+
49
+ # Sidebar for input fields
50
+ st.sidebar.header("Ask the AI Chatbot for Opportunities")
51
+
52
+ # Language selection
53
+ languages = {
54
+ "English": "English",
55
+ "Spanish": "Spanish",
56
+ "French": "French",
57
+ "German": "German",
58
+ "Italian": "Italian",
59
+ "Chinese": "Chinese",
60
+ "Japanese": "Japanese",
61
+ "Urdu": "Urdu"
62
+ }
63
+
64
+ selected_language = st.sidebar.selectbox("Select your preferred language:", list(languages.keys()))
65
+
66
+ # Chat history
67
+ if "messages" not in st.session_state:
68
+ st.session_state.messages = []
69
+
70
+ # Display chat history
71
+ for message in st.session_state.messages:
72
+ if message["role"] == "user":
73
+ st.markdown(f"**You:** {message['content']}")
74
+ else:
75
+ st.markdown(f"**AI:** {message['content']}")
76
+
77
+ # Input box for user query
78
+ user_query = st.text_input("Ask me about scholarships, internships, or online courses:")
79
+
80
+ # Button to send message to chatbot
81
+ if st.button("Send"):
82
+ if user_query:
83
+ # Append user query to chat history
84
+ st.session_state.messages.append({"role": "user", "content": user_query})
85
+
86
+ with st.spinner("Fetching opportunities..."):
87
+ # Get the opportunity details based on user query
88
+ opportunities = get_opportunities(user_query)
89
+
90
+ # Run the async translate function and get the translated text
91
+ translated_opportunities = asyncio.run(translate_text(opportunities, languages[selected_language]))
92
+
93
+ # Append AI response to chat history
94
+ st.session_state.messages.append({"role": "ai", "content": translated_opportunities})
95
+
96
+ # Scroll to the latest message
97
+ st.experimental_rerun()
98
+ else:
99
+ st.error("Please enter a query.")
100
+
101
+ # Add a footer with contact info and clickable links
102
+ st.markdown("""
103
+ <footer style="text-align:center; padding: 20px; font-size: 1rem; background-color: #f0f0f5;">
104
+ <p>Powered by Groq, Google Translate, and Streamlit</p>
105
+ <p>For more opportunities, visit:</p>
106
+ <ul style="list-style-type:none; padding: 0;">
107
+ <li><a href="https://www.groq.com" target="_blank">Groq - AI Solutions</a></li>
108
+ <li><a href="https://www.scholarships.com" target="_blank">Scholarships.com</a></li>
109
+ <li><a href="https://www.coursera.org" target="_blank">Coursera - Online Courses</a></li>
110
+ <li><a href="https://www.linkedin.com/jobs" target="_blank">LinkedIn Jobs</a></li>
111
+ </ul>
112
+ <p>Contact: [email protected]</p>
113
+ </footer>
114
+ """, unsafe_allow_html=True)