chat / app.py
DhruvDecoder's picture
Update app.py
fded1c8 verified
import os
import gradio as gr
from together import Together
# Function to interact with the model and process responses
def chatbot_response(query):
# Hugging Face API key setup (replace with your actual key)
api_key = os.getenv("TOGETHER_API_KEY")
if not api_key:
return "Error: API key not found. Please configure your TOGETHER_API_KEY."
client = Together(api_key=api_key)
# Define the system role prompt to guide the chatbot's behavior
system_message = {
"role": "system",
"content": ("You are a knowledgeable assistant providing accurate and concise information related to "
"technical fields, mentorship, alumni events, career guidance, academic support, placement "
"assistance, and other areas of interest to students. Focus on providing relevant information "
"for students and avoid unrelated topics. If something falls outside your area of expertise, "
"politely mention that it is not within your scope.")
}
# Chat request to Hugging Face model
response = client.chat.completions.create(
model="NousResearch/Hermes-3-Llama-3.1-405B-Turbo",
messages=[system_message, {"role": "user", "content": query}],
)
# Extract and return the response
return response.choices[0].message.content
# Gradio Interface
with gr.Blocks() as demo:
gr.Markdown("<h1>Information Assistant</h1>")
gr.Markdown("Ask me if you need any information or help.I am there to solve your queries!")
chatbot = gr.Chatbot()
query_input = gr.Textbox(placeholder="Type your question here (e.g., 'What is AI?What do I need to be an AI Engineer')")
def respond(query, chat_history):
# Get response from the chatbot
response = chatbot_response(query)
chat_history.append((query, response))
return chat_history, chat_history
query_input.submit(respond, [query_input, chatbot], [chatbot, chatbot])
# Launch the app
demo.launch()