-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
52 lines (43 loc) · 1.79 KB
/
app.py
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
import streamlit as st
import cohere
# Initialize Cohere Client with your API key
co = cohere.Client('hZIetHhwY0DTjffC7FsVVXaYkxUvuQ6f4hZnwcqi') # Replace with your actual API key
# Function to get responses from Cohere model for Q&A
def getCohereResponse(question, chat_history):
# Prompt Template for Q&A
prompt = f"""
You are a helpful and knowledgeable assistant. Answer the following question based on the previous conversation:
{chat_history}
Q: {question}
A:
"""
# Cohere generate API call
response = co.generate(
model='command-xlarge-nightly',
prompt=prompt,
max_tokens=100, # Adjust based on the length of the response you want
temperature=0.5, # Lower temperature for more focused answers
stop_sequences=["\n"], # Stop at the end of the answer
)
return response.generations[0].text if response.generations else "No answer generated"
# Streamlit UI
st.set_page_config(page_title="Bercho - Conversational Q&A Chatbot", page_icon="🤖")
st.title("Bercho - Conversational Q&A Chatbot 🤖")
# Initialize chat history in session state
if 'chat_history' not in st.session_state:
st.session_state['chat_history'] = ""
# User input
user_question = st.text_input("Ask me anything!")
# When user submits a question
if st.button("Ask"):
if user_question:
# Get the response from Cohere
chat_history = st.session_state['chat_history']
answer = getCohereResponse(user_question, chat_history)
# Update chat history
st.session_state['chat_history'] += f"Q: {user_question}\nA: {answer}\n\n"
# Display conversation history
st.subheader("Conversation")
st.text_area("", st.session_state['chat_history'], height=400)
else:
st.warning("Please enter a question.")