Referencer / app.py
Barat123's picture
Update app.py (#2)
11aa8c7
import gradio as gr
import re
# ----------- Conversion Functions -----------
def mla_to_apa(reference):
"""
Basic MLA 9 β†’ APA 7 converter (Books & simple journal cases)
Expected MLA format example:
Author Last, First. Title of Book. Publisher, Year.
"""
try:
# Extract Author
author_match = re.match(r"^(.*?),\s+(.*?)\.", reference)
if not author_match:
return "Unable to parse MLA format. Please check structure."
last_name = author_match.group(1).strip()
first_name = author_match.group(2).strip()
initials = "".join([name[0] + "." for name in first_name.split()])
# Extract Title
title_match = re.search(r"\.\s+(.*?)\.\s", reference)
title = title_match.group(1).strip() if title_match else ""
# Extract Publisher and Year
pub_year_match = re.search(r"\.\s+(.*?),\s+(\d{4})\.", reference)
publisher = pub_year_match.group(1).strip() if pub_year_match else ""
year = pub_year_match.group(2).strip() if pub_year_match else ""
apa_reference = f"{last_name}, {initials} ({year}). {title}. {publisher}."
return apa_reference
except Exception as e:
return f"Conversion error: {str(e)}"
def apa_to_mla(reference):
"""
Basic APA 7 β†’ MLA 9 converter
Expected APA format example:
Author, A. A. (Year). Title of book. Publisher.
"""
try:
# Extract Author
author_match = re.match(r"^(.*?),\s+(.*?)\s+\((\d{4})\)\.", reference)
if not author_match:
return "Unable to parse APA format. Please check structure."
last_name = author_match.group(1).strip()
initials = author_match.group(2).strip()
year = author_match.group(3).strip()
# Convert initials to placeholder first name (since APA doesn't store full)
first_name = initials.replace(".", "").strip()
# Extract Title
title_match = re.search(r"\)\.\s+(.*?)\.\s", reference)
title = title_match.group(1).strip() if title_match else ""
# Extract Publisher
publisher_match = re.search(r"\.\s+(.*?)\.$", reference)
publisher = publisher_match.group(1).strip() if publisher_match else ""
mla_reference = f"{last_name}, {first_name}. {title}. {publisher}, {year}."
return mla_reference
except Exception as e:
return f"Conversion error: {str(e)}"
def convert_reference(reference, source_format, target_format):
if source_format == "MLA 9th Edition" and target_format == "APA 7th Edition":
return mla_to_apa(reference)
elif source_format == "APA 7th Edition" and target_format == "MLA 9th Edition":
return apa_to_mla(reference)
else:
return "Conversion for selected formats not implemented yet."
# ----------- Gradio Interface -----------
with gr.Blocks(
title="Reference Format Converter",
theme=gr.themes.Soft(primary_hue="orange")
) as app:
gr.Markdown("## πŸ“š Reference Format Converter")
gr.Markdown("Convert references between MLA 9th Edition and APA 7th Edition.")
with gr.Group():
reference_input = gr.Textbox(
label="Paste Your Reference Here",
placeholder="Enter full reference...",
lines=4
)
with gr.Row():
source_format = gr.Dropdown(
choices=["MLA 9th Edition", "APA 7th Edition"],
label="Source Format",
value="MLA 9th Edition",
interactive=True
)
target_format = gr.Dropdown(
choices=["APA 7th Edition", "MLA 9th Edition"],
label="Target Format",
value="APA 7th Edition",
interactive=True
)
convert_btn = gr.Button(
"πŸ”„ Convert Reference",
variant="primary",
size="lg"
)
output = gr.Textbox(
label="Converted Reference",
lines=4,
interactive=False
)
convert_btn.click(
fn=convert_reference,
inputs=[reference_input, source_format, target_format],
outputs=output
)
app.launch()