yuvraj-yadav commited on
Commit
125237a
Β·
verified Β·
1 Parent(s): 21ff3fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -54
app.py CHANGED
@@ -1,67 +1,82 @@
1
- # app.py
2
 
3
  from sentence_transformers import SentenceTransformer
4
  from sklearn.metrics.pairwise import cosine_similarity
5
  import gradio as gr
 
 
 
6
 
7
- # Load fast transformer model
8
- model = SentenceTransformer('all-MiniLM-L6-v2') # Faster & free
9
 
10
- # Define math domains and descriptions
11
  DOMAINS = {
12
  "Real Analysis": "Studies properties of real-valued functions, sequences, limits, continuity, differentiation, Riemann/ Lebesgue integration, and convergence in the real number system.",
13
- "Complex Analysis": "Explores analytic functions of complex variables, contour integration, conformal mappings, and singularity theory.",
14
  "Functional Analysis": "Deals with infinite-dimensional vector spaces, Banach and Hilbert spaces, linear operators, duality, and spectral theory in the context of functional spaces.",
15
- "Measure Theory": "Studies sigma-algebras, measures, measurable functions, and integrals, forming the foundation for modern probability and real analysis.",
16
- "Fourier and Harmonic Analysis": "Analyzes functions via decompositions into sines, cosines, or general orthogonal bases, often involving Fourier series, Fourier transforms, and convolution techniques.",
17
- "Calculus of Variations": "Optimizes functionals over infinite-dimensional spaces, leading to Euler-Lagrange equations and applications in physics and control theory.",
18
- "Metric Geometry": "Explores geometric properties of metric spaces and the behavior of functions and sequences under various notions of distance.",
19
- "Ordinary Differential Equations (ODEs)": "Involves differential equations with functions of a single variable, their qualitative behavior, existence, uniqueness, and methods of solving them.",
20
- "Partial Differential Equations (PDEs)": "Deals with multivariable functions involving partial derivatives, including wave, heat, and Laplace equations.",
21
- "Dynamical Systems": "Studies evolution of systems over time using discrete or continuous-time equations, stability theory, phase portraits, and attractors.",
22
- "Linear Algebra": "Focuses on vector spaces, linear transformations, eigenvalues, diagonalization, and matrices.",
23
- "Abstract Algebra": "General study of algebraic structures such as groups, rings, fields, and modules.",
24
- "Group Theory": "Investigates algebraic structures with a single binary operation satisfying group axioms, including symmetry groups and applications.",
25
- "Ring and Module Theory": "Extends group theory to rings (two operations) and modules (generalized vector spaces).",
26
- "Field Theory": "Studies field extensions, algebraic and transcendental elements, and classical constructions.",
27
- "Galois Theory": "Connects field theory and group theory to solve polynomial equations and understand solvability.",
28
- "Algebraic Number Theory": "Applies tools from abstract algebra to study integers, Diophantine equations, and number fields.",
29
- "Representation Theory": "Studies abstract algebraic structures by representing their elements as linear transformations of vector spaces.",
30
- "Algebraic Geometry": "Examines solutions to polynomial equations using geometric and algebraic techniques like varieties, schemes, and morphisms.",
31
- "Differential Geometry": "Studies geometric structures on smooth manifolds, curvature, geodesics, and applications in general relativity.",
32
- "Topology": "Analyzes qualitative spatial properties preserved under continuous deformations, including homeomorphism, compactness, and connectedness.",
33
- "Geometric Topology": "Explores topological manifolds and their classification, knot theory, and low-dimensional topology.",
34
- "Symplectic Geometry": "Studies geometry arising from Hamiltonian systems and phase space, central to classical mechanics.",
35
- "Combinatorics": "Covers enumeration, existence, construction, and optimization of discrete structures.",
36
  "Graph Theory": "Deals with the study of graphs, networks, trees, connectivity, and coloring problems.",
37
- "Discrete Geometry": "Focuses on geometric objects and combinatorial properties in finite settings, such as polytopes and tilings.",
38
- "Set Theory": "Studies sets, cardinality, ordinals, ZFC axioms, and independence results.",
39
- "Mathematical Logic": "Includes propositional logic, predicate logic, proof theory, model theory, and recursion theory.",
40
- "Category Theory": "Provides a high-level, structural framework to relate different mathematical systems through morphisms and objects.",
41
- "Probability Theory": "Mathematical foundation for randomness, including random variables, distributions, expectation, and stochastic processes.",
42
- "Mathematical Statistics": "Theory behind estimation, hypothesis testing, confidence intervals, and likelihood inference.",
43
- "Stochastic Processes": "Studies processes that evolve with randomness over time, like Markov chains and Brownian motion.",
44
- "Information Theory": "Analyzes data transmission, entropy, coding theory, and information content in probabilistic settings.",
45
- "Numerical Analysis": "Designs and analyzes algorithms to approximate solutions of mathematical problems including root-finding, integration, and differential equations.",
46
  "Optimization": "Studies finding best outcomes under constraints, including convex optimization, linear programming, and integer programming.",
47
- "Operations Research": "Applies optimization, simulation, and probabilistic modeling to decision-making problems in logistics, finance, and industry.",
48
- "Control Theory": "Mathematically models and regulates dynamic systems through feedback and optimal control strategies.",
49
- "Computational Mathematics": "Applies algorithmic and numerical techniques to solve mathematical problems on computers.",
50
- "Game Theory": "Analyzes strategic interaction among rational agents using payoff matrices and equilibrium concepts.",
51
- "Machine Learning Theory": "Explores the mathematical foundation of algorithms that learn from data, covering generalization, VC dimension, and convergence.",
52
- "Spectral Theory": "Studies the spectrum (eigenvalues) of linear operators, primarily in Hilbert/Banach spaces, relevant to quantum mechanics and PDEs.",
53
- "Operator Theory": "Focuses on properties of linear operators on function spaces and their classification.",
54
- "Mathematical Physics": "Uses advanced mathematical tools to solve and model problems in physics, often involving differential geometry and functional analysis.",
55
- "Financial Mathematics": "Applies stochastic calculus and optimization to problems in pricing, risk, and investment.",
56
- "Mathematics Education": "Focuses on teaching methods, learning theories, and curriculum design in mathematics.",
57
- "History of Mathematics": "Studies the historical development of mathematical concepts, theorems, and personalities.",
58
- "Others / Multidisciplinary": "Covers problems that span multiple mathematical areas or do not fall neatly into a traditional domain."
59
  }
60
 
61
  domain_names = list(DOMAINS.keys())
62
  domain_texts = list(DOMAINS.values())
63
  domain_embeddings = model.encode(domain_texts)
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  def classify_math_question(question):
66
  q_embed = model.encode([question])
67
  scores = cosine_similarity(q_embed, domain_embeddings)[0]
@@ -70,16 +85,37 @@ def classify_math_question(question):
70
  minor = domain_names[sorted_indices[1]]
71
  major_reason = DOMAINS[major]
72
  minor_reason = DOMAINS[minor]
73
- explanation = f"<b>Major Domain:</b> {major}<br><i>Reason:</i> {major_reason}<br><br>" + \
74
- f"<b>Minor Domain:</b> {minor}<br><i>Reason:</i> {minor_reason}"
75
- return explanation
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
  iface = gr.Interface(
78
  fn=classify_math_question,
79
- inputs=gr.Textbox(lines=5, label="Enter Math Question (supports LaTeX)"),
80
- outputs=gr.HTML(label="Predicted Domains"),
81
- title="πŸ” Math Domain Classifier (Major + Minor)",
82
- description="Paste any math problem or LaTeX expression and get its major and minor domains with explanations."
83
  )
84
 
85
  iface.launch()
 
1
+ # app.py – Now includes DuckDuckGo, arXiv, and Semantic Scholar crawling
2
 
3
  from sentence_transformers import SentenceTransformer
4
  from sklearn.metrics.pairwise import cosine_similarity
5
  import gradio as gr
6
+ import arxiv
7
+ from semanticscholar import SemanticScholar
8
+ from duckduckgo_search import DDGS
9
 
10
+ # Load sentence transformer
11
+ model = SentenceTransformer('all-MiniLM-L6-v2')
12
 
13
+ # Math domain definitions (trimmed for brevity)
14
  DOMAINS = {
15
  "Real Analysis": "Studies properties of real-valued functions, sequences, limits, continuity, differentiation, Riemann/ Lebesgue integration, and convergence in the real number system.",
 
16
  "Functional Analysis": "Deals with infinite-dimensional vector spaces, Banach and Hilbert spaces, linear operators, duality, and spectral theory in the context of functional spaces.",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  "Graph Theory": "Deals with the study of graphs, networks, trees, connectivity, and coloring problems.",
18
+ "Partial Differential Equations (PDEs)": "Deals with multivariable functions involving partial derivatives, including wave, heat, and Laplace equations.",
 
 
 
 
 
 
 
 
19
  "Optimization": "Studies finding best outcomes under constraints, including convex optimization, linear programming, and integer programming.",
20
+ "Algebraic Geometry": "Examines solutions to polynomial equations using geometric and algebraic techniques like varieties, schemes, and morphisms.",
21
+ "Others": "Covers problems that span multiple mathematical areas or do not fall neatly into a traditional domain."
 
 
 
 
 
 
 
 
 
 
22
  }
23
 
24
  domain_names = list(DOMAINS.keys())
25
  domain_texts = list(DOMAINS.values())
26
  domain_embeddings = model.encode(domain_texts)
27
 
28
+ # Semantic Scholar + arXiv wrapper
29
+ sch = SemanticScholar()
30
+
31
+ def fetch_semantic_and_arxiv(query, top_n=5):
32
+ papers = []
33
+
34
+ # Semantic Scholar
35
+ try:
36
+ sem_results = sch.search_paper(query, limit=top_n)
37
+ for p in sem_results:
38
+ papers.append({
39
+ "title": p.title,
40
+ "authors": ", ".join(a.name for a in p.authors[:3]),
41
+ "year": p.year,
42
+ "url": p.url,
43
+ "source": "Semantic Scholar"
44
+ })
45
+ except:
46
+ pass
47
+
48
+ # arXiv
49
+ try:
50
+ search = arxiv.Search(query=query, max_results=top_n)
51
+ for r in search.results():
52
+ papers.append({
53
+ "title": r.title,
54
+ "authors": ", ".join(a.name for a in r.authors[:3]),
55
+ "year": r.published.year,
56
+ "url": r.entry_id,
57
+ "source": "arXiv"
58
+ })
59
+ except:
60
+ pass
61
+
62
+ return papers
63
+
64
+ def fetch_duckduckgo_links(query, max_results=5):
65
+ links = []
66
+ try:
67
+ with DDGS() as ddgs:
68
+ results = ddgs.text(query, max_results=max_results)
69
+ for res in results:
70
+ links.append({
71
+ "title": res['title'],
72
+ "url": res['href'],
73
+ "snippet": res['body'],
74
+ "source": "DuckDuckGo"
75
+ })
76
+ except:
77
+ pass
78
+ return links
79
+
80
  def classify_math_question(question):
81
  q_embed = model.encode([question])
82
  scores = cosine_similarity(q_embed, domain_embeddings)[0]
 
85
  minor = domain_names[sorted_indices[1]]
86
  major_reason = DOMAINS[major]
87
  minor_reason = DOMAINS[minor]
88
+
89
+ out = f"<b>Major Domain:</b> {major}<br><i>Reason:</i> {major_reason}<br><br>"
90
+ out += f"<b>Minor Domain:</b> {minor}<br><i>Reason:</i> {minor_reason}<br><br>"
91
+
92
+ refs = fetch_semantic_and_arxiv(question)
93
+ links = fetch_duckduckgo_links(question)
94
+
95
+ if refs:
96
+ out += "<b>Top Academic References:</b><ul>"
97
+ for p in refs:
98
+ out += f"<li><b>{p['title']}</b> ({p['year']}) - <i>{p['authors']}</i> [{p['source']}]<br><a href='{p['url']}' target='_blank'>{p['url']}</a></li>"
99
+ out += "</ul>"
100
+ else:
101
+ out += "<i>No academic references found.</i><br>"
102
+
103
+ if links:
104
+ out += "<b>Top Web Resources:</b><ul>"
105
+ for link in links:
106
+ out += f"<li><b>{link['title']}</b><br>{link['snippet']}<br><a href='{link['url']}' target='_blank'>{link['url']}</a></li>"
107
+ out += "</ul>"
108
+ else:
109
+ out += "<i>No web links found.</i>"
110
+
111
+ return out
112
 
113
  iface = gr.Interface(
114
  fn=classify_math_question,
115
+ inputs=gr.Textbox(lines=5, label="Enter Math Question (LaTeX supported)"),
116
+ outputs=gr.HTML(label="Predicted Domains + References"),
117
+ title="πŸ“š Math Domain Classifier with arXiv + Semantic Scholar + DuckDuckGo",
118
+ description="Paste any math problem or LaTeX expression and get its major/minor domains + academic references + web links."
119
  )
120
 
121
  iface.launch()