File size: 2,059 Bytes
10edaca
 
 
 
 
 
 
 
 
 
 
 
 
831d847
 
 
 
 
 
 
 
 
 
10edaca
 
 
831d847
10edaca
 
 
 
 
 
 
c879ec5
831d847
10edaca
 
fded1c8
10edaca
 
831d847
 
10edaca
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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()