File size: 2,630 Bytes
5ac46fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import urllib.request
import urllib.error
import os
from openai import OpenAI

# Function to fetch content from URL
def fetch_content(url, jina_api_key):
    full_url = f"https://r.jina.ai/{url}"
    headers = {
        'User-Agent': 'Mozilla/5.0',
        'Authorization': f'Bearer {jina_api_key}'
    }
    req = urllib.request.Request(full_url, headers=headers)
    with urllib.request.urlopen(req) as response:
        return response.read().decode('utf-8')

# Function to ask question using OpenAI
def ask_question(client, content, question):
    messages = [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": f"Here's some content:\n\n{content[:7000]}\n\nBased on this content, please answer the following question and be concise: {question}"}
    ]

    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=messages,
        max_tokens=700
    )

    return response.choices[0].message.content

# Streamlit app
def main():
    st.title("Web/PDF URL Content Q&A")

    # Get API keys from environment variables or Streamlit secrets
    jina_api_key = os.environ.get('JINA_API_KEY') or st.secrets["JINA_API_KEY"]
    openai_api_key = os.environ.get('OPENAI_API_KEY') or st.secrets["OPENAI_API_KEY"]

    if not jina_api_key or not openai_api_key:
        st.error("API keys are missing. Please set JINA_API_KEY and OPENAI_API_KEY.")
        return

    # Initialize OpenAI client
    client = OpenAI(api_key=openai_api_key)

    # URL input
    url = st.text_input("Enter the URL:")

    if url:
        try:
            # Fetch content
            with st.spinner("Fetching content..."):
                content = fetch_content(url, jina_api_key)
            st.success("Content fetched successfully!")

            # Question input
            question = st.text_input("Enter your question:")

            if st.button("Ask"):
                if question:
                    with st.spinner("Generating answer..."):
                        answer = ask_question(client, content, question)
                    st.subheader("Answer:")
                    st.write(answer)
                else:
                    st.warning("Please enter a question.")
        
        except urllib.error.HTTPError as e:
            st.error(f'HTTP Error {e.code}: {e.reason}. URL: {url}')
        except urllib.error.URLError as e:
            st.error(f'URL Error: {str(e)}. URL: {url}')
        except Exception as e:
            st.error(f'Unexpected error: {str(e)}. URL: {url}')

if __name__ == "__main__":
    main()