muhammadshaheryar commited on
Commit
b5b5b12
Β·
verified Β·
1 Parent(s): d82483e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from groq import Groq
3
+ import pdfplumber
4
+ import os
5
+ import gc
6
+ from dotenv import load_dotenv
7
+
8
+ load_dotenv()
9
+
10
+ # Initialize the Groq client
11
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
12
+
13
+ def generate_response_groq(context, query):
14
+ """Generate response using Groq API."""
15
+ prompt = f"Context: {context}\nQuestion: {query}\nAnswer:"
16
+ chat_completion = client.chat.completions.create(
17
+ messages=[
18
+ {
19
+ "role": "user",
20
+ "content": prompt,
21
+ }
22
+ ],
23
+ model="llama3-8b-8192",
24
+ )
25
+ response = chat_completion.choices[0].message.content
26
+ return response
27
+
28
+ def extract_text_from_pdf(pdf_file):
29
+ """Extract text from PDF file using pdfplumber."""
30
+ text = ""
31
+ with pdfplumber.open(pdf_file) as pdf:
32
+ for page in pdf.pages:
33
+ text += page.extract_text() or ""
34
+ return text
35
+
36
+ # Set the page layout to wide for better UI space
37
+ st.set_page_config(page_title="PDF Query Application", layout="wide")
38
+
39
+ # Sidebar
40
+ st.sidebar.title("PDF Query Assistant")
41
+ st.sidebar.image("https://upload.wikimedia.org/wikipedia/commons/thumb/a/af/PDF_file_icon.svg/1024px-PDF_file_icon.svg.png", use_column_width=True) # Adding an image in the sidebar
42
+ st.sidebar.markdown("### Navigation")
43
+ st.sidebar.markdown("Use this app to upload a PDF and ask questions about its content.")
44
+ st.sidebar.markdown("")
45
+
46
+ # Main UI layout
47
+ st.title("πŸ“„ PDF Query Application")
48
+ st.markdown("""
49
+ <style>
50
+ .main-content {background-color: #f0f2f6; padding: 20px; border-radius: 10px;}
51
+ .stButton>button {background-color: #4CAF50; color: white; font-size: 16px; border-radius: 10px;}
52
+ .stTextInput>div>div>input {background-color: #f0f2f6; color: black; border-radius: 5px;}
53
+ </style>
54
+ """, unsafe_allow_html=True)
55
+
56
+ st.markdown("<div class='main-content'>", unsafe_allow_html=True)
57
+
58
+ uploaded_file = st.file_uploader("Upload a PDF file", type="pdf")
59
+
60
+ if uploaded_file:
61
+ st.success("PDF uploaded successfully! πŸ“„")
62
+ document_text = extract_text_from_pdf(uploaded_file)
63
+ st.text_area("πŸ“œ Extracted Text", document_text, height=200)
64
+
65
+ query = st.text_input("πŸ” Enter your query")
66
+
67
+ if st.button("πŸ’¬ Get Answer"):
68
+ if query:
69
+ with st.spinner("Generating response..."):
70
+ response = generate_response_groq(document_text, query)
71
+ st.write("**Response:**")
72
+ st.write(response)
73
+
74
+ # Clear memory after generating response
75
+ gc.collect()
76
+ else:
77
+ st.error("Please enter a query.")
78
+
79
+ st.markdown("</div>", unsafe_allow_html=True)
80
+
81
+ # Footer
82
+ st.sidebar.markdown("### About")
83
+ st.sidebar.info("Developed with ❀️ using Streamlit and Groq API.")
84
+ st.sidebar.markdown("---")
85
+ st.sidebar.write("For more information, visit [Groq](https://www.groq.com) and [Streamlit](https://streamlit.io).")
86
+
87
+ # Customize the theme and color contrast
88
+ st.markdown("""
89
+ <style>
90
+ .css-1aumxhk {background-color: #E8EAF6;}
91
+ .stTextInput>div>div>input {border-color: #3f51b5;}
92
+ .stTextArea>div>div>textarea {border-color: #3f51b5;}
93
+ .stButton>button {background-color: #3f51b5; color: white; font-size: 16px;}
94
+ </style>
95
+ """, unsafe_allow_html=True)