saherPervaiz commited on
Commit
e95727f
·
verified ·
1 Parent(s): 9d6f7ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -41
app.py CHANGED
@@ -1,43 +1,134 @@
1
  import os
2
  import streamlit as st
3
- from groq import Groq # Correct import for the Groq client
4
-
5
- # Ensure the correct API key is set in the environment
6
- api_key = "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"
7
-
8
- # Streamlit interface
9
- st.title("AI Chatbot with Groq")
10
-
11
- # User input for the chatbot
12
- user_input = st.text_input("Ask something:", "")
13
-
14
- # Scholarship search option
15
- search_scholarships = st.checkbox("Search for scholarships")
16
-
17
- # If user asked about scholarships
18
- if search_scholarships:
19
- st.write("You can explore various scholarships for your education:")
20
- st.write("1. **Merit-based Scholarships**: Awarded based on academic performance.")
21
- st.write("2. **Need-based Scholarships**: For students who need financial assistance.")
22
- st.write("3. **Athletic Scholarships**: Given to students excelling in sports.")
23
- st.write("4. **Government Scholarships**: Offered by governments for local or international students.")
24
- st.write("5. **Private Scholarships**: Funded by organizations or businesses.")
25
-
26
- # Process user input to interact with the Groq API
27
- if user_input and not search_scholarships:
28
- try:
29
- # Initialize Groq client with the API key
30
- client = Groq(api_key=api_key)
31
-
32
- # Create a chat completion request using the user's input
33
- chat_completion = client.chat.completions.create(
34
- messages=[{"role": "user", "content": user_input}],
35
- model="llama-3.3-70b-versatile"
36
- )
37
-
38
- # Display the AI's response
39
- st.write("AI's response: ")
40
- st.write(chat_completion.choices[0].message.content)
41
-
42
- except Exception as e:
43
- st.error(f"Error: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_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
+ 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
+ # Function to get chatbot response
36
+ def get_chatbot_response(user_message):
37
+ # Fetch the API key from the environment variable
38
+ api_key = "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"
39
+
40
+ if not api_key:
41
+ raise ValueError("API key is missing. Make sure to set the GROQ_API_KEY environment variable.")
42
+
43
+ # Initialize the Groq client with the API key
44
+ client = Groq(api_key=api_key)
45
+
46
+ # Request to Groq API for chatbot response
47
+ response = client.chat.completions.create(
48
+ messages=[{"role": "user", "content": user_message}],
49
+ model="llama-3.3-70b-versatile",
50
+ )
51
+
52
+ return response.choices[0].message.content
53
+
54
+ # Streamlit App Interface
55
+ st.set_page_config(page_title="AI-Powered Opportunity Finder", page_icon=":bulb:", layout="wide")
56
+ st.title("AI-Powered Opportunity Finder for Youth")
57
+
58
+ # Custom CSS for improving the UI
59
+ st.markdown("""
60
+ <style>
61
+ .css-1v0mbdj {padding-top: 30px; font-size: 1.5rem;}
62
+ .css-1wa3m6h {padding: 10px; background-color: #f0f0f5;}
63
+ .css-1w4t6pm {border: 1px solid #ccc; padding: 10px; background-color: #fff;}
64
+ .css-1f4y1re {font-size: 1.2rem;}
65
+ </style>
66
+ """, unsafe_allow_html=True)
67
+
68
+ # Sidebar for input fields
69
+ st.sidebar.header("Provide your details to find opportunities")
70
+
71
+ # Collect user input for interests, skills, and location in the sidebar
72
+ interests = st.sidebar.text_input("Your Interests (e.g., AI, Robotics, Software Engineering):")
73
+ skills = st.sidebar.text_input("Your Skills (e.g., Python, Data Science, Web Development):")
74
+ location = st.sidebar.text_input("Your Location (e.g., Gujrat, Pakistan):")
75
+
76
+ # Language selection
77
+ languages = {
78
+ "English": "English",
79
+ "Spanish": "Spanish",
80
+ "French": "French",
81
+ "German": "German",
82
+ "Italian": "Italian",
83
+ "Chinese": "Chinese",
84
+ "Japanese": "Japanese",
85
+ "Urdu": "Urdu" # Full word for Urdu
86
+ }
87
+
88
+ selected_language = st.sidebar.selectbox("Select your preferred language:", list(languages.keys()))
89
+
90
+ # Container to display results
91
+ results_container = st.container()
92
+
93
+ # Chatbot container
94
+ chatbot_container = st.container()
95
+
96
+ # Button to fetch opportunities
97
+ if st.sidebar.button("Find Opportunities"):
98
+ if interests and skills and location:
99
+ with st.spinner("Fetching opportunities..."):
100
+ # Fetch recommendations using the Groq API
101
+ opportunities = get_opportunities(interests, skills, location)
102
+
103
+ # Run the async translate function and get the translated text
104
+ translated_opportunities = asyncio.run(translate_text(opportunities, languages[selected_language]))
105
+
106
+ # Display the opportunities
107
+ results_container.subheader("Recommended Opportunities")
108
+ results_container.write(translated_opportunities)
109
+ else:
110
+ st.sidebar.error("Please fill all fields.")
111
+
112
+ # Chatbot functionality
113
+ with chatbot_container:
114
+ st.subheader("AI Chatbot")
115
+ user_message = st.text_area("Ask anything to the chatbot:", "")
116
+ if user_message:
117
+ with st.spinner("Getting response from AI..."):
118
+ chatbot_response = get_chatbot_response(user_message)
119
+ st.write(f"**Chatbot Response**: {chatbot_response}")
120
+
121
+ # Add a footer with contact info and clickable links
122
+ st.markdown("""
123
+ <footer style="text-align:center; padding: 20px; font-size: 1rem; background-color: #f0f0f5;">
124
+ <p>Powered by Groq, Google Translate, and Streamlit</p>
125
+ <p>For more opportunities, visit:</p>
126
+ <ul style="list-style-type:none; padding: 0;">
127
+ <li><a href="https://www.groq.com" target="_blank">Groq - AI Solutions</a></li>
128
+ <li><a href="https://www.scholarships.com" target="_blank">Scholarships.com</a></li>
129
+ <li><a href="https://www.coursera.org" target="_blank">Coursera - Online Courses</a></li>
130
+ <li><a href="https://www.linkedin.com/jobs" target="_blank">LinkedIn Jobs</a></li>
131
+ </ul>
132
+ <p>Contact: [email protected]</p>
133
+ </footer>
134
+ """, unsafe_allow_html=True)