Update app.py
Browse files
app.py
CHANGED
@@ -1,82 +1,122 @@
|
|
1 |
import streamlit as st
|
2 |
import requests
|
|
|
3 |
|
4 |
# Set the page title and icon
|
5 |
-
st.set_page_config(page_title="Wikipedia Microbot
|
6 |
-
|
7 |
-
# Define a CSS style for better layout
|
8 |
-
st.markdown(
|
9 |
-
"""
|
10 |
-
<style>
|
11 |
-
.stTextInput, .stButton, .stProgress { margin: 10px 0; }
|
12 |
-
.stButton { width: 100%; }
|
13 |
-
.stAlert { padding: 10px; }
|
14 |
-
.stMarkdown img { max-width: 100%; }
|
15 |
-
.stText { font-weight: bold; }
|
16 |
-
</style>
|
17 |
-
""",
|
18 |
-
unsafe_allow_html=True
|
19 |
-
)
|
20 |
|
|
|
21 |
WIKIPEDIA_API_URL = "https://en.wikipedia.org/w/api.php"
|
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 |
else:
|
61 |
-
|
|
|
|
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
# Display the images using st.image
|
68 |
-
st.sidebar.image(image_url, caption=image['title'], use_column_width=True)
|
69 |
|
70 |
-
|
71 |
-
views = page.get("pageviews", {}).get(query, "Data not available")
|
72 |
-
views_text = f"Page Views (Last 7 days): {views}"
|
73 |
|
74 |
-
|
75 |
-
|
|
|
|
|
76 |
else:
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
-
|
82 |
-
|
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
+
import google.generativeai as palm
|
4 |
|
5 |
# Set the page title and icon
|
6 |
+
st.set_page_config(page_title="Wikipedia Microbot and AI Mode")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
# Wikipedia Microbot Constants
|
9 |
WIKIPEDIA_API_URL = "https://en.wikipedia.org/w/api.php"
|
10 |
|
11 |
+
# AI Mode Constants
|
12 |
+
palm.configure(api_key="AIzaSyCnrcQWTEedcGuEsJ9Uhbj_ZS9B2nozMiU")
|
13 |
+
defaults = {
|
14 |
+
'model': 'models/text-bison-001',
|
15 |
+
'temperature': 1,
|
16 |
+
'candidate_count': 1,
|
17 |
+
'top_k': 40,
|
18 |
+
'top_p': 0.95,
|
19 |
+
'max_output_tokens': 1024,
|
20 |
+
'stop_sequences': [],
|
21 |
+
'safety_settings': [
|
22 |
+
{"category": "HARM_CATEGORY_DEROGATORY", "threshold": 4},
|
23 |
+
{"category": "HARM_CATEGORY_TOXICITY", "threshold": 4},
|
24 |
+
{"category": "HARM_CATEGORY_VIOLENCE", "threshold": 4},
|
25 |
+
{"category": "HARM_CATEGORY_SEXUAL", "threshold": 4},
|
26 |
+
{"category": "HARM_CATEGORY_MEDICAL", "threshold": 4},
|
27 |
+
{"category": "HARM_CATEGORY_DANGEROUS", "threshold": 4},
|
28 |
+
]
|
29 |
+
}
|
30 |
+
|
31 |
+
# Custom Instruction
|
32 |
+
custom_instruction = """
|
33 |
+
I am a small system from HelpingAI made by Abhay Koul who is 16 years old and lives in KP Colony, Vessu, Anantnag. Abhay Koul is currently working on more AI and AI-related projects.Certainly! If you're looking for more information about Abhay Koul's projects and HelpingAI, I would recommend joining the Discord community. You can find additional details there. He has been working on AI from Jan 2023. If you want more details, you can join the Discord community. The main function of HelpingAI is to make learning and education easier and more convenient for students. My purpose is to simplify your learning journey by providing personalized assistance, innovative teaching methods, and tailored resources to meet your unique needs. I am here to make your educational experience more enjoyable and effective. Feel free to ask me any questions or let me know how I can assist you in your learning adventure and in many more things from your life.
|
34 |
+
"""
|
35 |
+
|
36 |
+
# Create tabs for Wikipedia Microbot and AI Mode
|
37 |
+
selected_mode = st.radio("Select Mode", ["Wikipedia Microbot", "AI Mode"])
|
38 |
+
|
39 |
+
if selected_mode == "Wikipedia Microbot":
|
40 |
+
# Wikipedia Microbot Code
|
41 |
+
st.title("Wikipedia Microbot")
|
42 |
+
st.markdown("Explore Wikipedia with Ease")
|
43 |
+
|
44 |
+
# Sidebar for user options
|
45 |
+
st.sidebar.header("Options")
|
46 |
+
|
47 |
+
# User input and search button
|
48 |
+
query = st.sidebar.text_input("Enter a Query", help="E.g., 'Python programming'")
|
49 |
+
search_button = st.sidebar.button("Search")
|
50 |
+
|
51 |
+
# Container for main content
|
52 |
+
main_container = st.container()
|
53 |
+
|
54 |
+
if search_button:
|
55 |
+
if query:
|
56 |
+
try:
|
57 |
+
# Search Wikipedia for the query
|
58 |
+
params = {
|
59 |
+
"action": "query",
|
60 |
+
"format": "json",
|
61 |
+
"prop": "extracts|info|pageviews",
|
62 |
+
"exintro": True,
|
63 |
+
"explaintext": True,
|
64 |
+
"exsectionformat": "plain",
|
65 |
+
"titles": query,
|
66 |
+
"utf8": 1,
|
67 |
+
"formatversion": 2,
|
68 |
+
"pvipdays": 7,
|
69 |
+
}
|
70 |
+
|
71 |
+
response = requests.get(WIKIPEDIA_API_URL, params=params)
|
72 |
+
|
73 |
+
if response.status_code == 200:
|
74 |
+
data = response.json()
|
75 |
+
|
76 |
+
if "error" in data:
|
77 |
+
st.sidebar.error(f"Error: {data['error']['info']}")
|
78 |
+
else:
|
79 |
+
page = data["query"]["pages"][0]
|
80 |
+
|
81 |
+
# Display page title
|
82 |
+
st.title(page['title'])
|
83 |
+
|
84 |
+
# Display page views statistics
|
85 |
+
views = page.get("pageviews", {}).get(query, "Data not available")
|
86 |
+
st.info(f"Page Views (Last 7 days): {views}")
|
87 |
+
|
88 |
+
# Display summary
|
89 |
+
st.write(page.get("extract", "No summary available."))
|
90 |
+
|
91 |
else:
|
92 |
+
st.sidebar.error("Error: Unable to retrieve data from Wikipedia. Please try again later.")
|
93 |
+
except Exception as e:
|
94 |
+
st.sidebar.error(f"Error: {e}")
|
95 |
|
96 |
+
elif selected_mode == "AI Mode":
|
97 |
+
# AI Mode Code
|
98 |
+
st.title("AI Mode")
|
99 |
+
st.markdown("Interact with an AI powered by Abhay Koul")
|
|
|
|
|
100 |
|
101 |
+
user_input = st.text_area('You:', height=100, help="Type your message here")
|
|
|
|
|
102 |
|
103 |
+
if st.button('Submit', key='ai_button'):
|
104 |
+
with st.spinner("Thinking..."):
|
105 |
+
if user_input.lower() in ['quit', 'exit', 'bye']:
|
106 |
+
st.write("Goodbye! Have a great day!")
|
107 |
else:
|
108 |
+
# Create a chat history session state
|
109 |
+
session_state = st.session_state.get(user_input, [])
|
110 |
+
session_state.append({"user": user_input})
|
111 |
+
st.session_state[user_input] = session_state
|
112 |
+
|
113 |
+
# Prepare conversation history
|
114 |
+
conversation_history = "\n".join(["You: " + item["user"] for item in session_state])
|
115 |
+
|
116 |
+
# Construct the prompt with conversation history
|
117 |
+
prompt = f"""{custom_instruction}
|
118 |
+
Your conversation history:\n{conversation_history}
|
119 |
+
Your AI-generated response:"""
|
120 |
|
121 |
+
response = palm.generate_text(**defaults, prompt=prompt)
|
122 |
+
st.write(response.result)
|