Spaces:
Sleeping
Sleeping
ariankhalfani
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -24,22 +24,11 @@ def extract_pdf_text(uploaded_file):
|
|
24 |
pdf_text += page.extract_text()
|
25 |
return pdf_text
|
26 |
|
27 |
-
def add_message_to_conversation(user_message, bot_message, model_name):
|
28 |
-
if "conversation" not in st.session_state:
|
29 |
-
st.session_state.conversation = []
|
30 |
-
st.session_state.conversation.append((user_message, bot_message, model_name))
|
31 |
-
|
32 |
# Streamlit app
|
33 |
st.set_page_config(page_title="Gemma 27B-it Chatbot Interface", layout="wide")
|
34 |
st.title("Gemma 27B-it Chatbot Interface")
|
35 |
st.write("Gemma 27B-it Chatbot Interface")
|
36 |
|
37 |
-
# Initialize session state for conversation and uploaded file
|
38 |
-
if "conversation" not in st.session_state:
|
39 |
-
st.session_state.conversation = []
|
40 |
-
if "uploaded_file" not in st.session_state:
|
41 |
-
st.session_state.uploaded_file = None
|
42 |
-
|
43 |
# File uploader for PDF
|
44 |
uploaded_file = st.file_uploader("Upload a PDF", type="pdf")
|
45 |
|
@@ -50,28 +39,15 @@ if uploaded_file:
|
|
50 |
st.write(pdf_text)
|
51 |
|
52 |
# User input for question
|
53 |
-
question = st.text_input("
|
54 |
|
55 |
# Handle user input and Gemma 27B-it model response
|
56 |
if st.button("Send") and question:
|
57 |
try:
|
58 |
with st.spinner("Waiting for the model to respond..."):
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
if isinstance(response, list):
|
64 |
-
answer = response[0].get("generated_text", "No response")
|
65 |
-
elif isinstance(response, dict):
|
66 |
-
answer = response.get("generated_text", "No response")
|
67 |
-
else:
|
68 |
-
answer = "No response"
|
69 |
-
|
70 |
-
# Add PDF text to the chat history
|
71 |
-
if st.session_state.uploaded_file:
|
72 |
-
chat_history += f"Document Text: {pdf_text}\n"
|
73 |
-
|
74 |
-
add_message_to_conversation(question, answer, "Gemma-2-27B-it")
|
75 |
except ValueError as e:
|
76 |
st.error(str(e))
|
77 |
|
@@ -79,36 +55,41 @@ if st.button("Send") and question:
|
|
79 |
st.markdown(
|
80 |
"""
|
81 |
<style>
|
82 |
-
.chat-bubble {
|
83 |
-
padding: 10px 14px;
|
84 |
-
border-radius: 14px;
|
85 |
-
margin-bottom: 10px;
|
86 |
-
display: inline-block;
|
87 |
-
max-width: 80%;
|
88 |
-
color: black;
|
89 |
-
}
|
90 |
-
.chat-bubble.user {
|
91 |
-
background-color: #dcf8c6;
|
92 |
-
align-self: flex-end;
|
93 |
-
}
|
94 |
-
.chat-bubble.bot {
|
95 |
-
background-color: #fff;
|
96 |
-
align-self: flex-start;
|
97 |
-
}
|
98 |
.chat-container {
|
99 |
display: flex;
|
100 |
flex-direction: column;
|
101 |
gap: 10px;
|
102 |
margin-top: 20px;
|
103 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
</style>
|
105 |
""",
|
106 |
unsafe_allow_html=True
|
107 |
)
|
108 |
|
109 |
# Display the conversation
|
110 |
-
st.
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
st.write('</div>', unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
pdf_text += page.extract_text()
|
25 |
return pdf_text
|
26 |
|
|
|
|
|
|
|
|
|
|
|
27 |
# Streamlit app
|
28 |
st.set_page_config(page_title="Gemma 27B-it Chatbot Interface", layout="wide")
|
29 |
st.title("Gemma 27B-it Chatbot Interface")
|
30 |
st.write("Gemma 27B-it Chatbot Interface")
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
# File uploader for PDF
|
33 |
uploaded_file = st.file_uploader("Upload a PDF", type="pdf")
|
34 |
|
|
|
39 |
st.write(pdf_text)
|
40 |
|
41 |
# User input for question
|
42 |
+
question = st.text_input("Ask a question...", "")
|
43 |
|
44 |
# Handle user input and Gemma 27B-it model response
|
45 |
if st.button("Send") and question:
|
46 |
try:
|
47 |
with st.spinner("Waiting for the model to respond..."):
|
48 |
+
response = query_model(GEMMA_27B_API_URL, {"inputs": question})
|
49 |
+
answer = response.get("generated_text", "No response")
|
50 |
+
st.write(f"**Gemma 27B-it:** {answer}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
except ValueError as e:
|
52 |
st.error(str(e))
|
53 |
|
|
|
55 |
st.markdown(
|
56 |
"""
|
57 |
<style>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
.chat-container {
|
59 |
display: flex;
|
60 |
flex-direction: column;
|
61 |
gap: 10px;
|
62 |
margin-top: 20px;
|
63 |
}
|
64 |
+
.user-message {
|
65 |
+
align-self: flex-end;
|
66 |
+
background-color: #dcf8c6;
|
67 |
+
padding: 10px 14px;
|
68 |
+
border-radius: 14px;
|
69 |
+
max-width: 80%;
|
70 |
+
}
|
71 |
+
.bot-message {
|
72 |
+
align-self: flex-start;
|
73 |
+
background-color: #fff;
|
74 |
+
padding: 10px 14px;
|
75 |
+
border-radius: 14px;
|
76 |
+
max-width: 80%;
|
77 |
+
}
|
78 |
</style>
|
79 |
""",
|
80 |
unsafe_allow_html=True
|
81 |
)
|
82 |
|
83 |
# Display the conversation
|
84 |
+
if st.session_state.conversation:
|
85 |
+
st.write('<div class="chat-container">', unsafe_allow_html=True)
|
86 |
+
for user_message, bot_message in st.session_state.conversation:
|
87 |
+
st.write(f'<div class="user-message">You: {user_message}</div>', unsafe_allow_html=True)
|
88 |
+
st.write(f'<div class="bot-message">Gemma 27B-it: {bot_message}</div>', unsafe_allow_html=True)
|
89 |
+
st.write('</div>', unsafe_allow_html=True)
|
90 |
+
|
91 |
+
# Add the current interaction to the conversation
|
92 |
+
if question:
|
93 |
+
st.session_state.conversation.append((question, answer))
|
94 |
+
else:
|
95 |
+
st.session_state.conversation = []
|