Spaces:
Sleeping
Sleeping
ai-kadhim
commited on
Commit
β’
9cb390e
1
Parent(s):
b003b25
uploaded app
Browse files
app.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PyPDF2 import PdfReader
|
3 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
4 |
+
from langchain.text_splitter import CharacterTextSplitter
|
5 |
+
from langchain.vectorstores import FAISS
|
6 |
+
from langchain.chains.question_answering import load_qa_chain
|
7 |
+
from langchain.llms import OpenAI
|
8 |
+
import os
|
9 |
+
|
10 |
+
os.environ["OPENAI_API_KEY"] = "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"
|
11 |
+
os.environ["SERPAPI_API_KEY"] = "8ccb7553d2c890be7bbbdc41e5ced77ab3732dfea760e42da3711778c98c074c"
|
12 |
+
|
13 |
+
|
14 |
+
# Define a function to load PDF and perform processing
|
15 |
+
def process_pdf(pdf_path):
|
16 |
+
pdfreader = PdfReader(pdf_path)
|
17 |
+
|
18 |
+
raw_text = ''
|
19 |
+
for page in pdfreader.pages:
|
20 |
+
content = page.extract_text()
|
21 |
+
if content:
|
22 |
+
raw_text += content
|
23 |
+
|
24 |
+
text_splitter = CharacterTextSplitter(
|
25 |
+
separator="\n",
|
26 |
+
chunk_size=800,
|
27 |
+
chunk_overlap=100,
|
28 |
+
length_function=len,
|
29 |
+
)
|
30 |
+
texts = text_splitter.split_text(raw_text)
|
31 |
+
|
32 |
+
embeddings = OpenAIEmbeddings()
|
33 |
+
document_search = FAISS.from_texts(texts, embeddings)
|
34 |
+
|
35 |
+
chain = load_qa_chain(OpenAI(), chain_type="stuff")
|
36 |
+
|
37 |
+
return document_search, chain
|
38 |
+
|
39 |
+
# Function to get yes/no emoji based on answer content
|
40 |
+
def get_answer_emoji(answer):
|
41 |
+
answer = answer.lower()
|
42 |
+
if "yes" in answer:
|
43 |
+
return "β
"
|
44 |
+
elif "no" in answer:
|
45 |
+
return "β"
|
46 |
+
else:
|
47 |
+
return "π‘"
|
48 |
+
|
49 |
+
# Streamlit UI
|
50 |
+
st.title("OrangePro AI - PDF and Text Analysis")
|
51 |
+
|
52 |
+
# Upload a PDF file
|
53 |
+
uploaded_pdf_file = st.file_uploader("Upload a PDF file for analysis", type=["pdf"])
|
54 |
+
uploaded_text_file = st.file_uploader("Upload a text file with questions (if available)", type=["txt"])
|
55 |
+
|
56 |
+
if uploaded_pdf_file:
|
57 |
+
st.subheader("Selected PDF Content")
|
58 |
+
|
59 |
+
# Display the content of the PDF
|
60 |
+
pdf_reader, qa_chain = process_pdf(uploaded_pdf_file)
|
61 |
+
|
62 |
+
st.write("PDF Content:")
|
63 |
+
st.text(pdf_reader)
|
64 |
+
|
65 |
+
if uploaded_text_file:
|
66 |
+
st.warning("Questions will be extracted from the uploaded text file. Disabling question input below.")
|
67 |
+
text_content = uploaded_text_file.read().decode('utf-8') # Decode bytes to string
|
68 |
+
questions = text_content.splitlines()
|
69 |
+
else:
|
70 |
+
# Allow the user to enter a list of questions
|
71 |
+
questions = st.text_area("Enter a list of questions (one per line):").split('\n')
|
72 |
+
|
73 |
+
if st.button("Analyze Questions"):
|
74 |
+
# Perform question answering for each question
|
75 |
+
st.subheader("Answers:")
|
76 |
+
answer_summary = []
|
77 |
+
yes_count = 0
|
78 |
+
total_questions = len(questions)
|
79 |
+
|
80 |
+
for question in questions:
|
81 |
+
if question.strip() == "":
|
82 |
+
continue
|
83 |
+
docs = pdf_reader.similarity_search(question)
|
84 |
+
answer = qa_chain.run(input_documents=docs, question=question)
|
85 |
+
|
86 |
+
emoji = get_answer_emoji(answer)
|
87 |
+
answer_summary.append([question, answer, emoji])
|
88 |
+
|
89 |
+
if emoji == "β
":
|
90 |
+
yes_count += 1
|
91 |
+
|
92 |
+
# Calculate and display the percentage of "yes" answers
|
93 |
+
if total_questions > 0:
|
94 |
+
yes_percentage = (yes_count / total_questions) * 100
|
95 |
+
else:
|
96 |
+
yes_percentage = 0
|
97 |
+
|
98 |
+
answer_summary.append(["Percentage of 'Yes' Answers", f"{yes_percentage:.2f}%", ""])
|
99 |
+
|
100 |
+
# Display the summary in a table
|
101 |
+
st.table(answer_summary)
|
102 |
+
|
103 |
+
# About section
|
104 |
+
st.sidebar.title("About OrangePro AI")
|
105 |
+
st.sidebar.info(
|
106 |
+
"OrangePro AI is an artificial intelligence testing and benchmarking platform for large language models (LLMs). It scores model performance based on real-world scenarios, allowing corporate clients such as Fortune 500 companies to choose the best model for their specific use cases."
|
107 |
+
"\n\n"
|
108 |
+
"The platform automates scoring, ranking model performance in real-world scenarios and key criteria like hallucinations and safety. OrangePro AI also automatically generates adversarial test suites at a large scale and benchmarks models to help customers identify the best model for specific use cases."
|
109 |
+
)
|
110 |
+
|
111 |
+
# Footer
|
112 |
+
st.sidebar.text("Powered by Streamlit and Langchain")
|