maaz21 commited on
Commit
0bbd893
·
verified ·
1 Parent(s): 170664f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -72
app.py CHANGED
@@ -1,77 +1,57 @@
1
- # app.py
2
  import streamlit as st
3
- import requests
4
- from bs4 import BeautifulSoup
5
- from openai import OpenAI # New-style OpenAI client for Groq
6
-
7
- # Set your Groq API key and base URL
8
- client = OpenAI(
9
- api_key="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", # Replace with your actual Groq API key
10
- base_url="https://api.groq.com/openai/v1"
11
- )
12
-
13
- def extract_text_from_url(url):
14
- try:
15
- response = requests.get(url)
16
- soup = BeautifulSoup(response.text, 'html.parser')
17
- paragraphs = soup.find_all('p')
18
- text = ' '.join([p.get_text() for p in paragraphs])
19
- return text
20
- except Exception as e:
21
- return f"Error fetching {url}: {e}"
22
-
23
- def generate_blog(content, keywords):
24
- prompt = f"""
25
- You are a professional SEO blog writer.
26
- Based on the following combined content, generate a completely new, attractive, and SEO-optimized blog.
27
- Please naturally incorporate the following keywords: {', '.join(keywords)}.
28
-
29
- Content:
30
- {content}
31
-
32
- Write the new blog post:
33
- """
34
- response = client.chat.completions.create(
35
- model="llama3-70b-8192", # LLaMA 3 via Groq
36
- messages=[
37
- {"role": "system", "content": "You are an expert SEO content writer."},
38
- {"role": "user", "content": prompt}
39
- ],
 
 
 
40
  temperature=0.7,
41
- max_tokens=1500
42
  )
43
- return response.choices[0].message.content
44
-
45
- def main():
46
- st.title("📝 Pro SEO Blog Writer")
47
-
48
- st.subheader("Enter three blog URLs:")
49
- url1 = st.text_input("Blog URL 1")
50
- url2 = st.text_input("Blog URL 2")
51
- url3 = st.text_input("Blog URL 3")
52
 
53
- st.subheader("Enter Target Keywords (comma separated):")
54
- keywords_input = st.text_input("Example: AI, machine learning, future technology")
55
-
56
- if st.button("Generate New Blog"):
57
- if url1 and url2 and url3 and keywords_input:
58
- with st.spinner("Extracting content and generating blog..."):
59
- content1 = extract_text_from_url(url1)
60
- content2 = extract_text_from_url(url2)
61
- content3 = extract_text_from_url(url3)
62
-
63
- combined_content = content1 + "\n\n" + content2 + "\n\n" + content3
64
- keywords = [kw.strip() for kw in keywords_input.split(",")]
65
-
66
- new_blog = generate_blog(combined_content, keywords)
67
-
68
- st.success("✅ Blog generated successfully!")
69
- st.subheader("Generated Blog:")
70
- st.write(new_blog)
71
-
72
- st.download_button("Download Blog as TXT", data=new_blog, file_name="seo_blog.txt")
73
- else:
74
- st.warning("Please fill in all fields.")
75
 
76
- if __name__ == "__main__":
77
- main()
 
 
 
 
 
 
1
  import streamlit as st
2
+ from langchain_community.document_loaders import PyPDFLoader
3
+ from langchain_community.vectorstores import FAISS
4
+ from langchain_community.embeddings import HuggingFaceEmbeddings
5
+ from langchain.chains import RetrievalQA
6
+ from langchain_community.llms import Groq
7
+ import os
8
+
9
+ # Set up page
10
+ st.set_page_config(page_title="Chat with PDF")
11
+
12
+ # Sidebar: enter Groq API key
13
+ st.sidebar.title("Configuration")
14
+ groq_api_key = st.sidebar.text_input("Enter your Groq API Key", type="password")
15
+
16
+ # Main heading
17
+ st.title("📄 PDF Chatbot using LangChain + FAISS + Groq")
18
+ st.markdown("This app answers your questions based on a predefined PDF.")
19
+
20
+ # Path to your preloaded PDF
21
+ PDF_PATH = "src/acca.pdf"
22
+
23
+ # Process PDF and create retriever (cached)
24
+ @st.cache_resource
25
+ def load_retriever(pdf_path):
26
+ loader = PyPDFLoader(pdf_path)
27
+ documents = loader.load_and_split()
28
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
29
+ db = FAISS.from_documents(documents, embeddings)
30
+ return db.as_retriever()
31
+
32
+ # Validate API key
33
+ if not groq_api_key:
34
+ st.warning("Please enter your Groq API key.")
35
+ else:
36
+ retriever = load_retriever(PDF_PATH)
37
+
38
+ # Initialize Groq LLM
39
+ llm = Groq(
40
+ model="llama3-8b-8192",
41
+ api_key=groq_api_key,
42
  temperature=0.7,
 
43
  )
 
 
 
 
 
 
 
 
 
44
 
45
+ # QA chain
46
+ qa_chain = RetrievalQA.from_chain_type(
47
+ llm=llm,
48
+ chain_type="stuff",
49
+ retriever=retriever
50
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
+ # Input field for questions
53
+ user_input = st.text_input("Ask a question about the PDF:")
54
+ if user_input:
55
+ with st.spinner("Generating response..."):
56
+ answer = qa_chain.run(user_input)
57
+ st.success(answer)