-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot.py
More file actions
62 lines (53 loc) · 2.09 KB
/
chatbot.py
File metadata and controls
62 lines (53 loc) · 2.09 KB
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
import streamlit as st
import requests
import json
# Function to call the local LLM service
def get_response(prompt):
url = "http://localhost:11434/api/generate"
headers = {"Content-Type": "application/json"}
payload = {
"model": "gemma:2b",
"prompt": prompt,
}
try:
response = requests.post(url, json=payload, headers=headers)
if response.status_code == 200:
response_text = ""
# Split response content by newline and process each JSON object
for line in response.iter_lines():
if line:
try:
response_data = json.loads(line)
response_text += response_data.get('response', '') + " "
except json.JSONDecodeError as e:
response_text += f"Error decoding JSON: {str(e)} "
return response_text.strip()
else:
return f"Error: {response.status_code} - {response.reason}"
except requests.exceptions.RequestException as e:
return f"Request error: {str(e)}"
st.title("Local LLM Chatbot")
if 'conversation' not in st.session_state:
st.session_state.conversation = []
# Sidebar
st.sidebar.header("Chatbot Options")
user_input = st.sidebar.text_area("You:", key="user_input", height=100)
if st.sidebar.button("Send"):
if user_input:
# Add user input to conversation
st.session_state.conversation.append({"role": "user", "text": user_input})
# Generate model response
with st.spinner("Generating response..."):
response_text = get_response(user_input)
# Add model response to conversation
st.session_state.conversation.append({"role": "bot", "text": response_text})
# Display conversation
st.subheader("Conversation")
for message in st.session_state.conversation:
if message["role"] == "user":
st.markdown(f"**You:** {message['text']}")
else:
st.markdown(f"**Bot:** {message['text']}")
# Clear conversation
if st.sidebar.button("Clear Conversation"):
st.session_state.conversation = []