AdelMessaoudi-13 commited on
Commit
1ba1ba2
Β·
0 Parent(s):

πŸš€ Math Research AI

Browse files
Files changed (7) hide show
  1. .gitattributes +35 -0
  2. .gitignore +4 -0
  3. README.md +70 -0
  4. app.py +26 -0
  5. filter_agent.py +55 -0
  6. requirements.txt +3 -0
  7. wiki_scraper.py +65 -0
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ __pycache__/
2
+ .DS_Store
3
+
4
+
README.md ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: MathResearchIA
3
+ emoji: πŸ’¬
4
+ colorFrom: yellow
5
+ colorTo: purple
6
+ sdk: gradio
7
+ sdk_version: 5.23.3
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ ---
12
+
13
+ # πŸ“š MathResearchIA
14
+
15
+ **MathResearchIA** makes learning mathematics easier by making key concepts accessible to everyone through clear and simplified explanations.
16
+ This project provides a user-friendly interface to explore **mathematical concepts** (e.g., *Fourier transform*, *Lie group*, *Lebesgue integral*, etc.) using summaries generated from Wikipedia articles.
17
+
18
+ ---
19
+
20
+ ## ✨ Key Features
21
+
22
+ - πŸ” **Smart filtering**: detects whether the topic is mathematical (via an autonomous AI agent).
23
+ - πŸ“– **Wikipedia search**: retrieves the full article related to the concept.
24
+ - 🧠 **AI-generated summary**: provides a clear and simplified explanation (powered by Gemini Flash).
25
+ - 🌐 **Source included**: each response includes a link to the corresponding Wikipedia article.
26
+ - ❌ **Explicit rejection**: if the topic is not mathematical, the query is rejected with a helpful message.
27
+
28
+ ---
29
+
30
+ ## πŸš€ How It Works
31
+
32
+ 1. Enter a **mathematical concept** (e.g., "Fourier transform").
33
+ 2. The autonomous AI agent checks whether it's a math-related topic.
34
+ 3. If valid:
35
+ - The Wikipedia article is retrieved.
36
+ - A simplified summary is generated.
37
+ - The source is displayed.
38
+ 4. Otherwise, the query is rejected with a message inviting you to correct or rephrase.
39
+
40
+ ---
41
+
42
+ ## πŸ§ͺ Tech Stack
43
+
44
+ - Python
45
+ - Gradio (web interface)
46
+ - `wikipedia-api` (for article retrieval)
47
+ - Gemini Flash (AI summarization)
48
+ - Autonomous agent (intelligent filtering)
49
+
50
+ ---
51
+
52
+ ## πŸ“Œ Project Purpose
53
+
54
+ This project explores the use of **autonomous agents** in focused AI applications, while also democratizing mathematics through clear, pedagogical, and reliable explanations.
55
+
56
+ ---
57
+
58
+ ## πŸ™Œ Contributions
59
+
60
+ Suggestions, ideas, and feedback are welcome! Feel free to fork the project, open a pull request, or contact me by [email](mailto:amessaoudi.am@gmail.com).
61
+
62
+ ---
63
+
64
+ ## πŸ”’ Note
65
+
66
+ **MathResearchIA** handles **mathematical concepts only**. If your input is rejected, check for typos or try rephrasing it for better detection.
67
+
68
+ ---
69
+
70
+ ✨ *A personal project by Adel Messaoudi – at the intersection of mathematics, pedagogy, and artificial intelligence.*
app.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from filter_agent import filter_math_query
3
+ from wiki_scraper import get_wikipedia_article
4
+
5
+ def math_explainer(topic):
6
+ """First checks if the topic is mathematical, corrects spelling, then fetches the full Wikipedia article and summarizes it."""
7
+
8
+ # πŸ” Step 1: Check if topic is math-related + Spelling correction
9
+ filter_result, corrected_topic = filter_math_query(topic)
10
+
11
+ if "❌" in filter_result: # If the query is rejected
12
+ return filter_result # Return the error message
13
+
14
+ # πŸ“‘ Step 2: Retrieve full Wikipedia article and summarize it
15
+ return get_wikipedia_article(corrected_topic)
16
+
17
+ iface = gr.Interface(
18
+ fn=math_explainer,
19
+ inputs="text",
20
+ outputs="text",
21
+ title="Math Research AI",
22
+ description="πŸš€ Math Research AI makes learning mathematics easier by making key concepts accessible to everyone through clear and simplified explanations.\n\nThis math-specialized AI agent analyzes your query, automatically corrects spelling errors, retrieves a Wikipedia article, and generates a simplified and understandable summary."
23
+ )
24
+
25
+ if __name__ == "__main__":
26
+ iface.launch()
filter_agent.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+
4
+ API_KEY = os.getenv("API_KEY_GEMINI")
5
+ GEMINI_URL = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={API_KEY}"
6
+
7
+ def filter_math_query(query):
8
+ """Checks if the query is related to a mathematical topic and corrects spelling mistakes using a single API call."""
9
+
10
+ if not API_KEY:
11
+ return "⚠️ Error: Missing API key. Please configure API_KEY_GEMINI.", None
12
+
13
+ headers = {"Content-Type": "application/json"}
14
+
15
+ prompt = (
16
+ f"Analyze the following query and perform two tasks:\n"
17
+ f"1️⃣ Check whether it strictly relates to mathematics.\n"
18
+ f" - Reply only with 'MATH' if it's a valid math-related topic.\n"
19
+ f" - Reply with 'NON MATH' if it's not a mathematical subject.\n"
20
+ f"2️⃣ If the query is mathematical, correct any spelling mistakes.\n"
21
+ f" - Respond in the format 'MATH\nCorrection: [Corrected query]'.\n"
22
+ f" - If the query is not mathematical, do not provide a correction.\n\n"
23
+ f"Query: {query}"
24
+ )
25
+
26
+ data = {
27
+ "contents": [
28
+ {
29
+ "parts": [
30
+ {"text": prompt}
31
+ ]
32
+ }
33
+ ]
34
+ }
35
+
36
+ try:
37
+ response = requests.post(GEMINI_URL, headers=headers, json=data)
38
+ response_json = response.json()
39
+ result = response_json["candidates"][0]["content"]["parts"][0]["text"].strip()
40
+
41
+ if result.startswith("MATH"):
42
+ corrected_query = query # Default to the original query
43
+ if "Correction:" in result:
44
+ corrected_query = result.split("Correction:")[1].strip()
45
+
46
+ return "βœ… Query accepted as mathematical.", corrected_query
47
+
48
+ elif "NON MATH" in result:
49
+ return "❌ Query rejected. This is not a mathematical topic.", None
50
+
51
+ else:
52
+ return f"⚠️ Error: Unexpected response from Gemini ({result})", None
53
+
54
+ except Exception as e:
55
+ return f"⚠️ Gemini API Error: {e}", None
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ wikipedia-api
2
+ gradio
3
+ requests
wiki_scraper.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import wikipediaapi
2
+ import os
3
+ import requests
4
+
5
+ API_KEY = os.getenv("API_KEY_GEMINI")
6
+ GEMINI_URL = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={API_KEY}"
7
+
8
+ def generate_summary(text):
9
+ """Generates a simplified summary of the text using the Gemini Flash API."""
10
+
11
+ if not API_KEY:
12
+ return "⚠️ Error: Missing API key. Please configure API_KEY_GEMINI."
13
+
14
+ headers = {"Content-Type": "application/json"}
15
+
16
+ text = text[:4000] # βœ… Send only the first 4000 characters
17
+
18
+ prompt = (
19
+ f"Here is an article extracted from Wikipedia:\n\n{text}\n\n"
20
+ "Generate a simplified summary that is easy to understand even for someone without an advanced background in mathematics. "
21
+ "Use an educational tone and rephrase complex concepts in simple terms. "
22
+ "The summary should be concise (5 to 7 sentences) and focus on the essential points."
23
+ )
24
+
25
+ data = {
26
+ "contents": [
27
+ {
28
+ "parts": [
29
+ {"text": prompt}
30
+ ]
31
+ }
32
+ ]
33
+ }
34
+
35
+ try:
36
+ response = requests.post(GEMINI_URL, headers=headers, json=data)
37
+ response_json = response.json()
38
+
39
+ if "candidates" not in response_json:
40
+ return "⚠️ Gemini API Error: Unexpected response. No text was returned."
41
+
42
+ result = response_json["candidates"][0]["content"]["parts"][0]["text"]
43
+ return result
44
+
45
+ except Exception as e:
46
+ return f"⚠️ Gemini API Error: {e}"
47
+
48
+ def get_wikipedia_article(topic):
49
+ """Fetches the full Wikipedia article and reformulates it with Gemini 2.0 Flash."""
50
+ user_agent = "MathResearchAI/1.0 (https://huggingface.co/spaces/AdelMessaoudi-13/MathResearchIA)"
51
+
52
+ wiki = wikipediaapi.Wikipedia(language="en", user_agent=user_agent)
53
+ page = wiki.page(topic)
54
+
55
+ if not page.exists():
56
+ return f"⚠️ No article found for '{topic}'."
57
+
58
+ raw_text = page.text # πŸ” Retrieve the full article
59
+
60
+ summary = generate_summary(raw_text) # πŸ” Summarize with Gemini
61
+
62
+ wikipedia_url = page.fullurl # πŸ”— Include Wikipedia source
63
+ summary_with_source = f"{summary}\n\nπŸ”— **Wikipedia Source**: {wikipedia_url}"
64
+
65
+ return summary_with_source