Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from langchain_community.document_loaders import PyPDFLoader | |
| import groq | |
| import os | |
| import tempfile | |
| # Try importing pypdf and pdfminer as backups | |
| try: | |
| import pypdf | |
| pypdf_installed = True | |
| except ImportError: | |
| pypdf_installed = False | |
| try: | |
| from pdfminer.high_level import extract_text | |
| pdfminer_installed = True | |
| except ImportError: | |
| pdfminer_installed = False | |
| # Basic Streamlit setup | |
| st.set_page_config(page_title="Research Assistant", layout="centered") | |
| st.title("π Research Assistant") | |
| # Initialize Groq client | |
| def get_groq_client(): | |
| api_key = os.environ.get("GROQ_API_KEY") | |
| if not api_key: | |
| st.error("GROQ_API_KEY is missing. Please set it in the environment variables.") | |
| return None | |
| try: | |
| return groq.Client(api_key=api_key) # β FIXED: No 'proxies' argument | |
| except Exception as e: | |
| st.error(f"Failed to initialize Groq client: {e}") | |
| return None | |
| # Function to analyze text | |
| def analyze_content(text): | |
| client = get_groq_client() | |
| if not client or not text: | |
| return "Analysis unavailable" | |
| try: | |
| response = client.chat.completions.create( | |
| messages=[ | |
| {"role": "system", "content": "You are a helpful research assistant"}, | |
| {"role": "user", "content": f"Summarize this research content:\n\n{text[:3000]}"} | |
| ], | |
| model="llama3-70b-8192", | |
| temperature=0.7, | |
| max_tokens=500 | |
| ) | |
| return response.choices[0].message.content | |
| except Exception as e: | |
| return f"Analysis failed: {e}" | |
| # Function to process PDF using either PyPDFLoader or pdfminer | |
| def process_pdf(file): | |
| with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as tmp: | |
| tmp.write(file.getvalue()) | |
| tmp_path = tmp.name | |
| try: | |
| if pypdf_installed: | |
| loader = PyPDFLoader(tmp_path) | |
| pages = loader.load() | |
| return " ".join(page.page_content for page in pages[:3]) # First 3 pages | |
| elif pdfminer_installed: | |
| return extract_text(tmp_path)[:3000] # Extract up to 3000 characters | |
| else: | |
| return "Neither pypdf nor pdfminer is installed. Please restart and rebuild the app." | |
| except Exception as e: | |
| return f"Error processing PDF: {e}" | |
| finally: | |
| os.unlink(tmp_path) | |
| # Main App | |
| tab1, tab2 = st.tabs(["Text Analysis", "PDF Analysis"]) | |
| with tab1: | |
| user_text = st.text_area("Enter research text:", height=150) | |
| if st.button("Analyze Text"): | |
| if user_text: | |
| with st.spinner("Analyzing..."): | |
| result = analyze_content(user_text) | |
| st.write(result) | |
| else: | |
| st.warning("Please enter some text") | |
| with tab2: | |
| uploaded_file = st.file_uploader("Upload PDF", type=["pdf"]) | |
| if uploaded_file and st.button("Analyze PDF"): | |
| with st.spinner("Processing PDF..."): | |
| pdf_text = process_pdf(uploaded_file) | |
| result = analyze_content(pdf_text) | |
| st.write(result) | |
| st.markdown("---") | |
| st.caption("Powered by Groq's Llama 3 70B") | |