Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,66 +1,46 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
from groq import Groq
|
| 4 |
-
from googletrans import Translator
|
| 5 |
-
import asyncio
|
| 6 |
-
from groq.client import Client as GroqClient
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
#
|
| 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":
|
| 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 |
-
#
|
| 38 |
-
|
| 39 |
-
|
| 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
|
| 51 |
with gr.Blocks() as demo:
|
| 52 |
-
gr.Markdown("#
|
| 53 |
-
|
| 54 |
with gr.Row():
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
| 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()
|