-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamlit.py
79 lines (66 loc) · 2.1 KB
/
streamlit.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
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
78
79
import streamlit as st
import pdfplumber
import requests
import os
from groq import Groq
def add_custom_css():
st.markdown("""
<style>
.body {
background-image: url('images.jpeg');
background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;
}
.stButton>button {
background-color: #4CAF50;
color: white;
border-radius: 8px;
padding: 15px 40px;
font-size: 18px;
display: block;
margin: 0 auto;
}
</style>
""", unsafe_allow_html=True)
add_custom_css()
def read_pdf(file):
with pdfplumber.open(file) as pdf:
first_page = pdf.pages[0]
return first_page.extract_text()
# Summarize the full text using Groq's Mistral model
def summarize_text(client, text):
try:
chat = client.chat.completions.create(
messages=[{
"role": "user",
"content": f"Summarize the following text: {text}"
}],
model="mixtral-8x7b-32768"
)
return chat.choices[0].message.content
except Exception as e:
st.write(f"Error during summarization: {str(e)}")
return f"Error: {str(e)}"
st.title("PDF Summarizer")
# File uploader to select the PDF
uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
# Extract text from the uploaded PDF
text = ""
if uploaded_file is not None:
text = read_pdf(uploaded_file)
st.text_area("Extracted Text:", text, height=300)
else:
st.write("No file uploaded")
# Initialize Groq client
client = Groq(api_key="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")
# Button to trigger summarization
if st.button("Summarize"):
if text:
# Summarize the entire text
summary = summarize_text(client, text)
# Display the summary
st.subheader("Summary:")
st.text_area("Summarized Text:", summary, height=300)
# streamlit run streamlit.py
#64 Groq api key