saherPervaiz commited on
Commit
529d9fe
·
verified ·
1 Parent(s): 3d0613b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -54
app.py CHANGED
@@ -1,66 +1,46 @@
1
  import gradio as gr
2
- import os
3
- from groq import Groq
4
- from googletrans import Translator
5
- import asyncio
6
- from groq.client import Client as GroqClient
7
 
 
 
 
8
 
9
- # Function to get recommendations from Groq AI based on user input
10
- def get_opportunities(user_interests, user_skills, user_location):
11
- # Fetch the API key from the environment variable
12
- api_key = "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"
13
-
14
- if not api_key:
15
- raise ValueError("API key is missing. Make sure to set the GROQ_API_KEY environment variable.")
16
-
17
- # Initialize the Groq client with the API key
18
- client = Groq(api_key=api_key)
19
-
20
- # Construct the query
21
- 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."
22
-
23
- # Request to Groq API
24
  response = client.chat.completions.create(
25
- messages=[{"role": "user", "content": query}],
26
- model="llama-3.3-70b-versatile",
27
  )
28
-
29
- return response.choices[0].message.content
30
-
31
- # Function to translate text into the selected language (async version)
32
- async def translate_text(text, target_language):
33
- translator = Translator()
34
- translated = await translator.translate(text, dest=target_language)
35
- return translated.text
36
 
37
- # Chatbot interface function
38
- def chatbot(input_text, interests, skills, location, language):
39
- if interests and skills and location:
40
- # Fetch recommendations using the Groq API
41
- opportunities = get_opportunities(interests, skills, location)
42
-
43
- # Run the async translate function and get the translated text
44
- translated_opportunities = asyncio.run(translate_text(opportunities, language))
45
-
46
- return translated_opportunities
47
- else:
48
- return "Please fill all fields: Interests, Skills, and Location."
49
 
50
- # Gradio interface components
51
  with gr.Blocks() as demo:
52
- gr.Markdown("# AI-Powered Opportunity Finder for Youth")
53
-
54
  with gr.Row():
55
- interests = gr.Textbox(label="Your Interests (e.g., AI, Robotics, Software Engineering):", placeholder="Enter your interests here...")
56
- skills = gr.Textbox(label="Your Skills (e.g., Python, Data Science, Web Development):", placeholder="Enter your skills here...")
57
- location = gr.Textbox(label="Your Location (e.g., Gujrat, Pakistan):", placeholder="Enter your location here...")
58
-
59
- language = gr.Dropdown(label="Select your preferred language:", choices=["English", "Spanish", "French", "German", "Italian", "Chinese", "Japanese", "Urdu"], value="English")
60
-
61
- with gr.Chatbot() as chatbot_output:
62
- submit_button = gr.Button("Find Opportunities")
63
- submit_button.click(chatbot, inputs=[gr.Textbox(), interests, skills, location, language], outputs=chatbot_output)
 
 
 
64
 
65
  # Launch the app
66
  demo.launch()
 
1
  import gradio as gr
2
+ from groq.client import Client # Adjust this based on Groq's official documentation
 
 
 
 
3
 
4
+ # Initialize Groq API client
5
+ api_key = "your_groq_api_key" # Replace with your actual API key
6
+ client = Client(api_key=api_key)
7
 
8
+ # Chatbot logic using Groq API
9
+ def chatbot(message, history):
10
+ history = history or [] # Initialize history if None
11
+
12
+ # Build a prompt using the chat history
13
+ prompt = "\n".join([f"User: {msg[0]}\nBot: {msg[1]}" for msg in history])
14
+ prompt += f"\nUser: {message}\nBot:"
15
+
16
+ # Query the Groq API
 
 
 
 
 
 
17
  response = client.chat.completions.create(
18
+ messages=[{"role": "user", "content": prompt}],
19
+ model="llama-3.3-70b-versatile", # Adjust the model name if necessary
20
  )
21
+ bot_reply = response.choices[0].message.content.strip() # Extract the reply
 
 
 
 
 
 
 
22
 
23
+ # Append the conversation to history
24
+ history.append((message, bot_reply))
25
+ return history, history
 
 
 
 
 
 
 
 
 
26
 
27
+ # Gradio Interface
28
  with gr.Blocks() as demo:
29
+ gr.Markdown("# Gradio Chatbot with Groq API")
30
+ chatbot_ui = gr.Chatbot(label="Chatbot") # Chatbot display
31
  with gr.Row():
32
+ user_input = gr.Textbox(placeholder="Type your message here...") # Input box
33
+ submit_btn = gr.Button("Send") # Send button
34
+
35
+ # Clear history button
36
+ clear_btn = gr.Button("Clear Chat")
37
+
38
+ # State management for chat history
39
+ state = gr.State()
40
+
41
+ # Event bindings
42
+ submit_btn.click(chatbot, inputs=[user_input, state], outputs=[chatbot_ui, state])
43
+ clear_btn.click(lambda: ([], None), outputs=[chatbot_ui, state])
44
 
45
  # Launch the app
46
  demo.launch()