Spaces:
Build error
Build error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +57 -39
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,58 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from langchain.chat_models import ChatOpenAI
|
| 3 |
+
from langchain.memory import ConversationBufferMemory
|
| 4 |
+
from langchain.chains import ConversationChain
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Set your OpenAI API Key
|
| 8 |
+
os.environ["OPENAI_API_KEY"] = "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"
|
| 9 |
+
|
| 10 |
+
# π OpenRouter base URL
|
| 11 |
+
OPENROUTER_BASE_URL = "https://openrouter.ai/api/v1"
|
| 12 |
+
|
| 13 |
+
# π€ Choose model (you can change this)
|
| 14 |
+
MODEL_NAME = "openai/gpt-oss-120b:free"
|
| 15 |
+
|
| 16 |
+
# Initialize LLM
|
| 17 |
+
llm = ChatOpenAI(
|
| 18 |
+
model=MODEL_NAME,
|
| 19 |
+
temperature=0.7,
|
| 20 |
+
base_url=OPENROUTER_BASE_URL
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# Memory (stores conversation)
|
| 25 |
+
memory = ConversationBufferMemory()
|
| 26 |
+
|
| 27 |
+
# Conversation chain
|
| 28 |
+
conversation = ConversationChain(
|
| 29 |
+
llm=llm,
|
| 30 |
+
memory=memory,
|
| 31 |
+
verbose=True
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Streamlit UI
|
| 35 |
+
st.set_page_config(page_title="Simple Chatbot", page_icon="π€")
|
| 36 |
+
st.title("π€ Simple LangChain Chatbot")
|
| 37 |
+
|
| 38 |
+
# Session state for chat history
|
| 39 |
+
if "chat_history" not in st.session_state:
|
| 40 |
+
st.session_state.chat_history = []
|
| 41 |
+
|
| 42 |
+
# User input
|
| 43 |
+
user_input = st.text_input("You:", placeholder="Ask something...")
|
| 44 |
+
|
| 45 |
+
if user_input:
|
| 46 |
+
# Get response from LangChain
|
| 47 |
+
response = conversation.predict(input=user_input)
|
| 48 |
+
|
| 49 |
+
# Store chat
|
| 50 |
+
st.session_state.chat_history.append(("You", user_input))
|
| 51 |
+
st.session_state.chat_history.append(("Bot", response))
|
| 52 |
+
|
| 53 |
+
# Display chat history
|
| 54 |
+
for role, text in st.session_state.chat_history:
|
| 55 |
+
if role == "You":
|
| 56 |
+
st.markdown(f"**π§ You:** {text}")
|
| 57 |
+
else:
|
| 58 |
+
st.markdown(f"**π€ Bot:** {text}")
|