Spaces:
Sleeping
Sleeping
# app.py – Now includes DuckDuckGo, arXiv, and Semantic Scholar crawling | |
from sentence_transformers import SentenceTransformer | |
from sklearn.metrics.pairwise import cosine_similarity | |
import gradio as gr | |
import arxiv | |
from semanticscholar import SemanticScholar | |
from duckduckgo_search import DDGS | |
# Load sentence transformer | |
model = SentenceTransformer('all-MiniLM-L6-v2') | |
# Math domain definitions (trimmed for brevity) | |
DOMAINS = { | |
"Real Analysis": "Studies properties of real-valued functions, sequences, limits, continuity, differentiation, Riemann/ Lebesgue integration, and convergence in the real number system.", | |
"Complex Analysis": "Explores analytic functions of complex variables, contour integration, conformal mappings, and singularity theory.", | |
"Functional Analysis": "Deals with infinite-dimensional vector spaces, Banach and Hilbert spaces, linear operators, duality, and spectral theory in the context of functional spaces.", | |
"Measure Theory": "Studies sigma-algebras, measures, measurable functions, and integrals, forming the foundation for modern probability and real analysis.", | |
"Fourier and Harmonic Analysis": "Analyzes functions via decompositions into sines, cosines, or general orthogonal bases, often involving Fourier series, Fourier transforms, and convolution techniques.", | |
"Calculus of Variations": "Optimizes functionals over infinite-dimensional spaces, leading to Euler-Lagrange equations and applications in physics and control theory.", | |
"Metric Geometry": "Explores geometric properties of metric spaces and the behavior of functions and sequences under various notions of distance.", | |
"Ordinary Differential Equations (ODEs)": "Involves differential equations with functions of a single variable, their qualitative behavior, existence, uniqueness, and methods of solving them.", | |
"Partial Differential Equations (PDEs)": "Deals with multivariable functions involving partial derivatives, including wave, heat, and Laplace equations.", | |
"Dynamical Systems": "Studies evolution of systems over time using discrete or continuous-time equations, stability theory, phase portraits, and attractors.", | |
"Linear Algebra": "Focuses on vector spaces, linear transformations, eigenvalues, diagonalization, and matrices.", | |
"Abstract Algebra": "General study of algebraic structures such as groups, rings, fields, and modules.", | |
"Group Theory": "Investigates algebraic structures with a single binary operation satisfying group axioms, including symmetry groups and applications.", | |
"Ring and Module Theory": "Extends group theory to rings (two operations) and modules (generalized vector spaces).", | |
"Field Theory": "Studies field extensions, algebraic and transcendental elements, and classical constructions.", | |
"Galois Theory": "Connects field theory and group theory to solve polynomial equations and understand solvability.", | |
"Algebraic Number Theory": "Applies tools from abstract algebra to study integers, Diophantine equations, and number fields.", | |
"Representation Theory": "Studies abstract algebraic structures by representing their elements as linear transformations of vector spaces.", | |
"Algebraic Geometry": "Examines solutions to polynomial equations using geometric and algebraic techniques like varieties, schemes, and morphisms.", | |
"Differential Geometry": "Studies geometric structures on smooth manifolds, curvature, geodesics, and applications in general relativity.", | |
"Topology": "Analyzes qualitative spatial properties preserved under continuous deformations, including homeomorphism, compactness, and connectedness.", | |
"Geometric Topology": "Explores topological manifolds and their classification, knot theory, and low-dimensional topology.", | |
"Symplectic Geometry": "Studies geometry arising from Hamiltonian systems and phase space, central to classical mechanics.", | |
"Combinatorics": "Covers enumeration, existence, construction, and optimization of discrete structures.", | |
"Graph Theory": "Deals with the study of graphs, networks, trees, connectivity, and coloring problems.", | |
"Discrete Geometry": "Focuses on geometric objects and combinatorial properties in finite settings, such as polytopes and tilings.", | |
"Set Theory": "Studies sets, cardinality, ordinals, ZFC axioms, and independence results.", | |
"Mathematical Logic": "Includes propositional logic, predicate logic, proof theory, model theory, and recursion theory.", | |
"Category Theory": "Provides a high-level, structural framework to relate different mathematical systems through morphisms and objects.", | |
"Probability Theory": "Mathematical foundation for randomness, including random variables, distributions, expectation, and stochastic processes.", | |
"Mathematical Statistics": "Theory behind estimation, hypothesis testing, confidence intervals, and likelihood inference.", | |
"Stochastic Processes": "Studies processes that evolve with randomness over time, like Markov chains and Brownian motion.", | |
"Information Theory": "Analyzes data transmission, entropy, coding theory, and information content in probabilistic settings.", | |
"Numerical Analysis": "Designs and analyzes algorithms to approximate solutions of mathematical problems including root-finding, integration, and differential equations.", | |
"Optimization": "Studies finding best outcomes under constraints, including convex optimization, linear programming, and integer programming.", | |
"Operations Research": "Applies optimization, simulation, and probabilistic modeling to decision-making problems in logistics, finance, and industry.", | |
"Control Theory": "Mathematically models and regulates dynamic systems through feedback and optimal control strategies.", | |
"Computational Mathematics": "Applies algorithmic and numerical techniques to solve mathematical problems on computers.", | |
"Game Theory": "Analyzes strategic interaction among rational agents using payoff matrices and equilibrium concepts.", | |
"Machine Learning Theory": "Explores the mathematical foundation of algorithms that learn from data, covering generalization, VC dimension, and convergence.", | |
"Spectral Theory": "Studies the spectrum (eigenvalues) of linear operators, primarily in Hilbert/Banach spaces, relevant to quantum mechanics and PDEs.", | |
"Operator Theory": "Focuses on properties of linear operators on function spaces and their classification.", | |
"Mathematical Physics": "Uses advanced mathematical tools to solve and model problems in physics, often involving differential geometry and functional analysis.", | |
"Financial Mathematics": "Applies stochastic calculus and optimization to problems in pricing, risk, and investment.", | |
"Mathematics Education": "Focuses on teaching methods, learning theories, and curriculum design in mathematics.", | |
"History of Mathematics": "Studies the historical development of mathematical concepts, theorems, and personalities.", | |
"Others / Multidisciplinary": "Covers problems that span multiple mathematical areas or do not fall neatly into a traditional domain." | |
} | |
domain_names = list(DOMAINS.keys()) | |
domain_texts = list(DOMAINS.values()) | |
domain_embeddings = model.encode(domain_texts) | |
def fetch_arxiv_refs(query, max_results=3): | |
refs = [] | |
try: | |
search = arxiv.Search(query=query, max_results=max_results) | |
for r in search.results(): | |
refs.append({ | |
"title": r.title, | |
"authors": ", ".join(a.name for a in r.authors[:3]), | |
"year": r.published.year, | |
"url": r.entry_id, | |
"source": "arXiv" | |
}) | |
except: | |
pass | |
return refs | |
def fetch_duckduckgo_links(query, max_results=3): | |
links = [] | |
try: | |
with DDGS() as ddgs: | |
results = ddgs.text(query, max_results=max_results) | |
for res in results: | |
links.append({ | |
"title": res['title'], | |
"url": res['href'], | |
"snippet": res['body'], | |
"source": "DuckDuckGo" | |
}) | |
except: | |
pass | |
return links | |
def classify_math_question(question): | |
q_embed = model.encode([question]) | |
scores = cosine_similarity(q_embed, domain_embeddings)[0] | |
sorted_indices = scores.argsort()[::-1] | |
major = domain_names[sorted_indices[0]] | |
minor = domain_names[sorted_indices[1]] | |
major_reason = DOMAINS[major] | |
minor_reason = DOMAINS[minor] | |
out = f"<b>Major Domain:</b> {major}<br><i>Reason:</i> {major_reason}<br><br>" | |
out += f"<b>Minor Domain:</b> {minor}<br><i>Reason:</i> {minor_reason}<br><br>" | |
refs = fetch_arxiv_refs(question, max_results=3) | |
links = fetch_duckduckgo_links(question, max_results=3) | |
if refs: | |
out += "<b>Top Academic References (arXiv):</b><ul>" | |
for p in refs: | |
out += f"<li><b>{p['title']}</b> ({p['year']}) - <i>{p['authors']}</i><br><a href='{p['url']}' target='_blank'>{p['url']}</a></li>" | |
out += "</ul>" | |
else: | |
out += "<i>No academic references found.</i><br>" | |
if links: | |
out += "<b>Top Web Resources (DuckDuckGo):</b><ul>" | |
for link in links: | |
out += f"<li><b>{link['title']}</b><br>{link['snippet']}<br><a href='{link['url']}' target='_blank'>{link['url']}</a></li>" | |
out += "</ul>" | |
else: | |
out += "<i>No web links found.</i>" | |
return out | |
iface = gr.Interface( | |
fn=classify_math_question, | |
inputs=gr.Textbox(lines=5, label="Enter Math Question (LaTeX supported)"), | |
outputs=gr.HTML(label="Predicted Domains + References"), | |
title="⚡ Fast Math Domain Classifier with arXiv + DuckDuckGo", | |
description="Classifies math problems into major/minor domains and fetches fast references from arXiv + DuckDuckGo." | |
) | |
iface.launch() |