File size: 1,806 Bytes
9e5df6e
 
 
 
d5ac857
9e5df6e
 
 
d5ac857
9e5df6e
 
d5ac857
 
 
9e5df6e
d5ac857
9e5df6e
 
 
 
 
420143c
9e5df6e
420143c
9e5df6e
 
 
 
 
420143c
9e5df6e
 
 
 
420143c
9e5df6e
420143c
9e5df6e
 
 
 
420143c
 
9e5df6e
420143c
 
 
 
e5fa4c0
420143c
 
 
9e5df6e
a521172
10ca710
 
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
# app.py
import streamlit as st
import os
from dotenv import load_dotenv
from langchain.chat_models import ChatOpenAI

# Load HuggingFace API token
load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

# Initialize the HuggingFace LLM
llm = ChatOpenAI(
    openai_api_key=OPENAI_API_KEY,
    model_name="gpt-4o-mini",
    temperature=0.7,
    max_tokens=50
)

# Streamlit UI setup
st.set_page_config(page_title="🧠 HuggingFace Chatbot", page_icon="πŸ€–")
st.title("πŸ€– HuggingFace Chatbot")
st.caption("Built with Streamlit + LangChain (50-word max answers)")

# Initialize chat history
if "messages" not in st.session_state:
    st.session_state.messages = [
        {"role": "assistant", "content": "Hi there! Ask me anything."}
    ]

# Display chat messages
for msg in st.session_state.messages:
    with st.chat_message(msg["role"]):
        st.markdown(msg["content"])

# Chat input
if prompt := st.chat_input("Type your message here..."):
    # Add user message
    st.session_state.messages.append({"role": "user", "content": prompt})
    with st.chat_message("user"):
        st.markdown(prompt)

    # Construct prompt (only user + assistant, formatted)
    conversation = "You are a helpful assistant. Keep replies within 50 words.\n\n"
    for msg in st.session_state.messages:
        if msg["role"] == "user":
            conversation += f"User: {msg['content']}\n"
        elif msg["role"] == "assistant":
            continue  # Don't include previous assistant replies

    conversation += "Assistant:"  # Prompt the model to continue

    # Generate model response
    with st.chat_message("assistant"):
        response = llm.invoke(conversation)
        st.markdown(response.content)
        st.session_state.messages.append({"role": "assistant", "content": response.content})