Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import os
|
2 |
import requests
|
3 |
import streamlit as st
|
|
|
4 |
|
5 |
# Get the Hugging Face API Token from environment variables
|
6 |
HF_API_TOKEN = os.getenv("HF_API_KEY")
|
@@ -16,6 +17,13 @@ def query_model(api_url, payload):
|
|
16 |
response = requests.post(api_url, headers=HEADERS, json=payload)
|
17 |
return response.json()
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
def add_message_to_conversation(user_message, bot_message, model_name):
|
20 |
if "conversation" not in st.session_state:
|
21 |
st.session_state.conversation = []
|
@@ -26,9 +34,20 @@ st.set_page_config(page_title="Gemma 27B-it Chatbot Interface", layout="wide")
|
|
26 |
st.title("Gemma 27B-it Chatbot Interface")
|
27 |
st.write("Gemma 27B-it Chatbot Interface")
|
28 |
|
29 |
-
# Initialize session state for conversation
|
30 |
if "conversation" not in st.session_state:
|
31 |
st.session_state.conversation = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
# User input for question
|
34 |
question = st.text_input("Question", placeholder="Enter your question here...")
|
@@ -40,7 +59,7 @@ if st.button("Send") and question:
|
|
40 |
# Construct the chat history
|
41 |
chat_history = " ".join([msg[1] for msg in st.session_state.conversation[-5:]]) + f"User: {question}\n"
|
42 |
response = query_model(GEMMA_27B_API_URL, {"inputs": chat_history})
|
43 |
-
|
44 |
if isinstance(response, list):
|
45 |
answer = response[0].get("generated_text", "No response")
|
46 |
elif isinstance(response, dict):
|
@@ -48,6 +67,10 @@ if st.button("Send") and question:
|
|
48 |
else:
|
49 |
answer = "No response"
|
50 |
|
|
|
|
|
|
|
|
|
51 |
add_message_to_conversation(question, answer, "Gemma-2-27B-it")
|
52 |
except ValueError as e:
|
53 |
st.error(str(e))
|
|
|
1 |
import os
|
2 |
import requests
|
3 |
import streamlit as st
|
4 |
+
import PyMuPDF
|
5 |
|
6 |
# Get the Hugging Face API Token from environment variables
|
7 |
HF_API_TOKEN = os.getenv("HF_API_KEY")
|
|
|
17 |
response = requests.post(api_url, headers=HEADERS, json=payload)
|
18 |
return response.json()
|
19 |
|
20 |
+
def extract_pdf_text(uploaded_file):
|
21 |
+
pdf_text = ""
|
22 |
+
pdf_doc = PyMuPDF.open(uploaded_file)
|
23 |
+
for page_num in range(len(pdf_doc)):
|
24 |
+
pdf_text += pdf_doc.getPageText(page_num)
|
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 = []
|
|
|
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 |
+
|
46 |
+
# Handle PDF upload and text extraction
|
47 |
+
if uploaded_file:
|
48 |
+
pdf_text = extract_pdf_text(uploaded_file)
|
49 |
+
st.write("### PDF Text Extracted:")
|
50 |
+
st.write(pdf_text)
|
51 |
|
52 |
# User input for question
|
53 |
question = st.text_input("Question", placeholder="Enter your question here...")
|
|
|
59 |
# Construct the chat history
|
60 |
chat_history = " ".join([msg[1] for msg in st.session_state.conversation[-5:]]) + f"User: {question}\n"
|
61 |
response = query_model(GEMMA_27B_API_URL, {"inputs": chat_history})
|
62 |
+
|
63 |
if isinstance(response, list):
|
64 |
answer = response[0].get("generated_text", "No response")
|
65 |
elif isinstance(response, dict):
|
|
|
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))
|