Abhaykoul commited on
Commit
10bba5c
1 Parent(s): a276094

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -69
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", page_icon=":mag:")
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
- st.title("Wikipedia Microbot")
24
-
25
- # Create a sidebar for better organization
26
- st.sidebar.header("Options")
27
-
28
- # Create widgets in the sidebar
29
- query = st.sidebar.text_input("Enter a Query")
30
- search_button = st.sidebar.button("Search")
31
- clear_output_button = st.sidebar.button("Clear Output")
32
-
33
- # Create a container for the main content
34
- main_container = st.container()
35
-
36
- if search_button:
37
- if query:
38
- try:
39
- # Search Wikipedia for the query
40
- params = {
41
- "action": "query",
42
- "format": "json",
43
- "prop": "extracts|images|info|pageviews",
44
- "exintro": True,
45
- "explaintext": True,
46
- "exsectionformat": "plain",
47
- "titles": query,
48
- "utf8": 1,
49
- "formatversion": 2,
50
- "pvipdays": 7, # Get page views statistics for the last 7 days
51
- }
52
-
53
- response = requests.get(WIKIPEDIA_API_URL, params=params)
54
-
55
- if response.status_code == 200:
56
- data = response.json()
57
-
58
- if "error" in data:
59
- st.sidebar.error(f"Error: {data['error']['info']}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  else:
61
- page = data["query"]["pages"][0]
 
 
62
 
63
- summary = page.get("extract", "")
64
- html_content = ""
65
- for image in page.get("images", []):
66
- image_url = f"https://en.wikipedia.org/wiki/{image['title']}"
67
- # Display the images using st.image
68
- st.sidebar.image(image_url, caption=image['title'], use_column_width=True)
69
 
70
- # Display page views statistics
71
- views = page.get("pageviews", {}).get(query, "Data not available")
72
- views_text = f"Page Views (Last 7 days): {views}"
73
 
74
- result = f"# {page['title']}\n{views_text}\n{summary}\n{html_content}"
75
- main_container.markdown(result, unsafe_allow_html=True)
 
 
76
  else:
77
- st.sidebar.error("Error: Unable to retrieve data from Wikipedia. Please try again later.")
78
- except Exception as e:
79
- st.sidebar.error(f"Error: {e}")
 
 
 
 
 
 
 
 
 
80
 
81
- if clear_output_button:
82
- main_container.text("Output Cleared")
 
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)