create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import urllib.request
|
3 |
+
import urllib.error
|
4 |
+
import os
|
5 |
+
from openai import OpenAI
|
6 |
+
|
7 |
+
# Function to fetch content from URL
|
8 |
+
def fetch_content(url, jina_api_key):
|
9 |
+
full_url = f"https://r.jina.ai/{url}"
|
10 |
+
headers = {
|
11 |
+
'User-Agent': 'Mozilla/5.0',
|
12 |
+
'Authorization': f'Bearer {jina_api_key}'
|
13 |
+
}
|
14 |
+
req = urllib.request.Request(full_url, headers=headers)
|
15 |
+
with urllib.request.urlopen(req) as response:
|
16 |
+
return response.read().decode('utf-8')
|
17 |
+
|
18 |
+
# Function to ask question using OpenAI
|
19 |
+
def ask_question(client, content, question):
|
20 |
+
messages = [
|
21 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
22 |
+
{"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}"}
|
23 |
+
]
|
24 |
+
|
25 |
+
response = client.chat.completions.create(
|
26 |
+
model="gpt-4o-mini",
|
27 |
+
messages=messages,
|
28 |
+
max_tokens=700
|
29 |
+
)
|
30 |
+
|
31 |
+
return response.choices[0].message.content
|
32 |
+
|
33 |
+
# Streamlit app
|
34 |
+
def main():
|
35 |
+
st.title("Web/PDF URL Content Q&A")
|
36 |
+
|
37 |
+
# Get API keys from environment variables or Streamlit secrets
|
38 |
+
jina_api_key = os.environ.get('JINA_API_KEY') or st.secrets["JINA_API_KEY"]
|
39 |
+
openai_api_key = os.environ.get('OPENAI_API_KEY') or st.secrets["OPENAI_API_KEY"]
|
40 |
+
|
41 |
+
if not jina_api_key or not openai_api_key:
|
42 |
+
st.error("API keys are missing. Please set JINA_API_KEY and OPENAI_API_KEY.")
|
43 |
+
return
|
44 |
+
|
45 |
+
# Initialize OpenAI client
|
46 |
+
client = OpenAI(api_key=openai_api_key)
|
47 |
+
|
48 |
+
# URL input
|
49 |
+
url = st.text_input("Enter the URL:")
|
50 |
+
|
51 |
+
if url:
|
52 |
+
try:
|
53 |
+
# Fetch content
|
54 |
+
with st.spinner("Fetching content..."):
|
55 |
+
content = fetch_content(url, jina_api_key)
|
56 |
+
st.success("Content fetched successfully!")
|
57 |
+
|
58 |
+
# Question input
|
59 |
+
question = st.text_input("Enter your question:")
|
60 |
+
|
61 |
+
if st.button("Ask"):
|
62 |
+
if question:
|
63 |
+
with st.spinner("Generating answer..."):
|
64 |
+
answer = ask_question(client, content, question)
|
65 |
+
st.subheader("Answer:")
|
66 |
+
st.write(answer)
|
67 |
+
else:
|
68 |
+
st.warning("Please enter a question.")
|
69 |
+
|
70 |
+
except urllib.error.HTTPError as e:
|
71 |
+
st.error(f'HTTP Error {e.code}: {e.reason}. URL: {url}')
|
72 |
+
except urllib.error.URLError as e:
|
73 |
+
st.error(f'URL Error: {str(e)}. URL: {url}')
|
74 |
+
except Exception as e:
|
75 |
+
st.error(f'Unexpected error: {str(e)}. URL: {url}')
|
76 |
+
|
77 |
+
if __name__ == "__main__":
|
78 |
+
main()
|