Spaces:
Sleeping
Sleeping
File size: 2,330 Bytes
7a6101e bc64242 9735286 bc64242 9735286 7a6101e bc64242 ced19be bc64242 |
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
from dotenv import load_dotenv
import os
import streamlit as st
import openai
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Document
from sentence_transformers import CrossEncoder
import fitz # PyMuPDF library for PDF processing
import tempfile
load_dotenv()
openai.api_key = st.secrets['api_key']
# Create a sidebar
st.sidebar.title("Model Configuration")
# File uploader moved to the sidebar
uploaded_file = st.sidebar.file_uploader("Upload a PDF", type=["pdf"])
# Option menu for model selection
model_selection = st.sidebar.selectbox("Model Selection", ["GPT 3.5", "LLama 2"])
# Slider for selecting model temperature
model_temperature = st.sidebar.slider("Select model temperature", 0.0, 0.5, 1.0)
# Initialize LLM response storage
llm_responses = []
# Initialize HHEM model
hhem_model = CrossEncoder('vectara/hallucination_evaluation_model')
if uploaded_file is not None:
# Save the uploaded PDF file to a temporary location
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_pdf:
temp_pdf.write(uploaded_file.read())
temp_pdf_path = temp_pdf.name
# Open the PDF file using PyMuPDF
pdf_document = fitz.open(temp_pdf_path)
text = ""
for page_number in range(pdf_document.page_count):
page = pdf_document[page_number]
text += page.get_text()
documents = [Document(text=text)]
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
query = st.text_input("Ask your question")
button = st.button("Ask")
if button:
print(query)
response = query_engine.query(query)
st.write(response.response)
# Record LLM response
llm_responses.append(response.response)
# Calculate and display HHEM score for each LLM response
for i, llm_response in enumerate(llm_responses):
score = hhem_model.predict([text, llm_response])
st.sidebar.write(f"Response {i + 1} - HHEM Score: {score}")
# Close and remove the temporary PDF file
pdf_document.close()
os.remove(temp_pdf_path)
# Display LLM responses
if llm_responses:
st.sidebar.markdown("## LLM Responses")
for i, llm_response in enumerate(llm_responses):
st.sidebar.write(f"Response {i + 1}: {llm_response}")
|