saherPervaiz commited on
Commit
08caf0d
·
verified ·
1 Parent(s): 995bd60

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -84
app.py CHANGED
@@ -1,64 +1,72 @@
1
  import asyncio
2
- import os
3
  import streamlit as st
4
  from googletrans import Translator
5
  from groq import Groq
 
6
 
7
- # Initialize the Groq API client
8
- client = Groq(api_key="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")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- # Async function for translation
11
- async def translate_text(text, target_language):
12
  translator = Translator()
13
- # Translate text asynchronously
14
- translated = await translator.translate(text, dest=target_language)
15
  return translated.text
16
 
17
- # Fetch opportunities based on user input (interests, skills, location)
18
- def get_opportunities(interests, skills, location):
19
- # Placeholder for real opportunity fetching logic
20
- # In your case, this would use Groq or another API to get dynamic data
21
- return {
22
- 'scholarships': 'Here are some scholarship opportunities...',
23
- 'internships': 'Here are some internship opportunities...',
24
- 'courses': 'Here are some courses you can explore...'
25
- }
26
-
27
- # Async function to get translated opportunities
28
- async def get_translated_opportunities(opportunities, selected_language):
29
- translated_opportunities = {}
30
-
31
- # Translate each opportunity
32
- for opportunity_type, opportunity_list in opportunities.items():
33
- translated_opportunities[opportunity_type] = await translate_text(opportunity_list, selected_language)
34
-
35
- return translated_opportunities
36
-
37
  # Streamlit App Interface
38
- def main():
39
- st.set_page_config(page_title="AI-Powered Opportunity Finder", page_icon=":bulb:", layout="wide")
40
- st.title("AI-Powered Opportunity Finder for Youth")
41
 
42
- # Custom CSS for improving the UI
43
- st.markdown("""
44
- <style>
45
- .css-1v0mbdj {padding-top: 30px; font-size: 1.5rem;}
46
- .css-1wa3m6h {padding: 10px; background-color: #f0f0f5;}
47
- .css-1w4t6pm {border: 1px solid #ccc; padding: 10px; background-color: #fff;}
48
- .css-1f4y1re {font-size: 1.2rem;}
49
- </style>
50
- """, unsafe_allow_html=True)
51
 
52
- # Sidebar for input fields
53
- st.sidebar.header("Provide your details to find opportunities")
54
 
55
- # Collect user input for interests, skills, and location in the sidebar
56
- interests = st.sidebar.text_input("Your Interests (e.g., AI, Robotics, Software Engineering):")
57
- skills = st.sidebar.text_input("Your Skills (e.g., Python, Data Science, Web Development):")
58
- location = st.sidebar.text_input("Your Location (e.g., Gujrat, Pakistan):")
59
 
60
- # Language selection
61
- languages = {
62
  "English": "English",
63
  "Spanish": "Spanish",
64
  "French": "French",
@@ -69,43 +77,27 @@ def main():
69
  "Urdu": "Urdu" # Full word for Urdu
70
  }
71
 
72
- selected_language = st.sidebar.selectbox("Select your preferred language:", list(languages.keys()))
73
 
74
- # Container to display results
75
- results_container = st.container()
76
 
77
- # Button to fetch opportunities
78
- if st.sidebar.button("Find Opportunities"):
79
- if interests and skills and location:
80
- with st.spinner("Fetching opportunities..."):
81
- # Fetch recommendations using the Groq API
82
- opportunities = get_opportunities(interests, skills, location)
83
-
84
- # Async translation of opportunities
85
- translated_opportunities = asyncio.run(get_translated_opportunities(opportunities, languages[selected_language]))
86
 
87
- # Display the opportunities
88
- results_container.subheader(f"Recommended Opportunities in {selected_language}")
89
- for opportunity_type, translated_text in translated_opportunities.items():
90
- results_container.markdown(f"**{opportunity_type.capitalize()}**: {translated_text}")
91
- else:
92
- st.sidebar.error("Please fill all fields.")
93
-
94
- # Add a footer with contact info and clickable links
95
- st.markdown("""
96
- <footer style="text-align:center; padding: 20px; font-size: 1rem; background-color: #f0f0f5;">
97
- <p>Powered by Groq, Google Translate, and Streamlit</p>
98
- <p>For more opportunities, visit:</p>
99
- <ul style="list-style-type:none; padding: 0;">
100
- <li><a href="https://www.groq.com" target="_blank">Groq - AI Solutions</a></li>
101
- <li><a href="https://www.scholarships.com" target="_blank">Scholarships.com</a></li>
102
- <li><a href="https://www.coursera.org" target="_blank">Coursera - Online Courses</a></li>
103
- <li><a href="https://www.linkedin.com/jobs" target="_blank">LinkedIn Jobs</a></li>
104
- </ul>
105
- <p>Contact: support@example.com</p>
106
- </footer>
107
- """, unsafe_allow_html=True)
108
-
109
- # Run the app
110
- if __name__ == "__main__":
111
- main()
 
1
  import asyncio
 
2
  import streamlit as st
3
  from googletrans import Translator
4
  from groq import Groq
5
+ import os
6
 
7
+ # Function to get recommendations from Groq AI based on user input
8
+ def get_opportunities(user_interests, user_skills, user_location):
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 interests in {user_interests}, skills in {user_skills}, and location of {user_location}, find scholarships, internships, online courses, and career advice suitable for them."
20
+
21
+ # Request to Groq API
22
+ try:
23
+ response = client.chat.completions.create(
24
+ messages=[{"role": "user", "content": query}],
25
+ model="llama-3.3-70b-versatile",
26
+ )
27
+
28
+ # Debugging: Check the response
29
+ st.write(response) # Display the raw response for debugging
30
+
31
+ if response and hasattr(response, 'choices') and response.choices:
32
+ content = response.choices[0].message.content
33
+ return content
34
+ else:
35
+ return "Sorry, no opportunities found at the moment."
36
+ except Exception as e:
37
+ st.error(f"Error while fetching data: {e}")
38
+ return "Sorry, we encountered an issue fetching the data."
39
 
40
+ # Function to translate text into the selected language
41
+ def translate_text(text, target_language):
42
  translator = Translator()
43
+ translated = translator.translate(text, dest=target_language)
 
44
  return translated.text
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  # Streamlit App Interface
47
+ st.set_page_config(page_title="AI-Powered Opportunity Finder", page_icon=":bulb:", layout="wide")
48
+ st.title("AI-Powered Opportunity Finder for Youth")
 
49
 
50
+ # Custom CSS for improving the UI
51
+ st.markdown("""
52
+ <style>
53
+ .css-1v0mbdj {padding-top: 30px; font-size: 1.5rem;}
54
+ .css-1wa3m6h {padding: 10px; background-color: #f0f0f5;}
55
+ .css-1w4t6pm {border: 1px solid #ccc; padding: 10px; background-color: #fff;}
56
+ .css-1f4y1re {font-size: 1.2rem;}
57
+ </style>
58
+ """, unsafe_allow_html=True)
59
 
60
+ # Sidebar for input fields
61
+ st.sidebar.header("Provide your details to find opportunities")
62
 
63
+ # Collect user input for interests, skills, and location in the sidebar
64
+ interests = st.sidebar.text_input("Your Interests (e.g., AI, Robotics, Software Engineering):")
65
+ skills = st.sidebar.text_input("Your Skills (e.g., Python, Data Science, Web Development):")
66
+ location = st.sidebar.text_input("Your Location (e.g., Gujrat, Pakistan):")
67
 
68
+ # Language selection
69
+ languages = {
70
  "English": "English",
71
  "Spanish": "Spanish",
72
  "French": "French",
 
77
  "Urdu": "Urdu" # Full word for Urdu
78
  }
79
 
80
+ selected_language = st.sidebar.selectbox("Select your preferred language:", list(languages.keys()))
81
 
82
+ # Container to display results
83
+ results_container = st.container()
84
 
85
+ # Button to fetch opportunities
86
+ if st.sidebar.button("Find Opportunities"):
87
+ if interests and skills and location:
88
+ with st.spinner("Fetching opportunities..."):
89
+ # Fetch recommendations using the Groq API
90
+ opportunities = get_opportunities(interests, skills, location)
 
 
 
91
 
92
+ # Display the raw opportunities for debugging
93
+ st.subheader("Raw Opportunities Data:")
94
+ st.write(opportunities) # Display raw data
95
+
96
+ # Translate the opportunities based on the selected language
97
+ translated_opportunities = translate_text(opportunities, languages[selected_language])
98
+
99
+ # Display the opportunities
100
+ results_container.subheader("Recommended Opportunities")
101
+ results_container.write(translated_opportunities)
102
+ else:
103
+ st.sidebar.error("Please fill all fields.")