File size: 1,980 Bytes
4557e2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f157d1
4557e2a
 
 
 
 
 
 
 
 
 
b228709
 
 
 
 
 
 
4557e2a
 
b228709
4557e2a
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import os
import gradio as gr
from langchain_groq import ChatGroq
from langchain_core.prompts import ChatPromptTemplate

# --------- SYSTEM PROMPT ----------
system_prompt_ai_teacher = (
    "You are a helpful, polite assistant who can be a potential study buddy."
)

# --------- LOAD API KEY ----------
groq_api_key = os.getenv("GROQ_API_KEY")

llm = ChatGroq(
    model_name="openai/gpt-oss-120b",
    temperature=0.7,
    groq_api_key=groq_api_key
)

prompt = ChatPromptTemplate.from_messages(
    [
        ("system", system_prompt_ai_teacher),
        ("human", "{user_input}")
    ]
)

chain = prompt | llm

# --------- CHAT FUNCTION ----------
def predict(message, history):
    response = chain.invoke({"user_input": message})
    return response.content

# --------- GRADIO UI ----------
with gr.Blocks(
    theme=gr.themes.Soft(),
    css="""
    body, .gradio-container {
        font-family: 'Inter', 'Segoe UI', 'Roboto', 'Helvetica Neue', Arial, sans-serif;
    }

    .chatbot {
        height: 500px;
        font-size: 16px;
    }

    footer {
        visibility: hidden;
    }
    """
) as demo:

    gr.Markdown(
        """
        # HERE AND NOW AI — Study Buddy  
        Your friendly AI assistant for learning and exploration
        """
    )

    chatbot = gr.Chatbot(
        label="AI Chat",
        elem_classes="chatbot"
    )

    msg = gr.Textbox(
        placeholder="Ask me anything...",
        show_label=False
    )

    clear = gr.Button("🗑️ Clear Chat")

    def respond(message, history):
        # Add user message
        history = history or []
        history.append({"role": "user", "content": message})
        # Get model response
        bot_response = predict(message, history)
        # Add assistant message
        history.append({"role": "assistant", "content": bot_response})
        return "", history


    msg.submit(respond, [msg, chatbot], [msg, chatbot])
    clear.click(lambda: [], None, chatbot)

demo.launch()