ops / app.py
Militaryint's picture
Update app.py
ed5b451 verified
import os
import openai
import gradio as gr
from datetime import datetime
# ------------------------------------------------------------
# App Identity
# ------------------------------------------------------------
APP_TITLE = "Parampara & Prayog — Hindi Sahitya Adhyayan & Anusandhān Kendra"
APP_DESCRIPTION = (
"A digital mentorship and research space inspired by the academic legacy "
"of Presidency University’s Faculty of Arts — bringing together Parampara (Tradition) "
"and Prayog (Experiment) in Hindi Studies."
)
# ------------------------------------------------------------
# OpenAI API Key Setup
# ------------------------------------------------------------
openai.api_key = os.getenv("OPENAI_API_KEY")
# ------------------------------------------------------------
# Professor Voice Prompt
# ------------------------------------------------------------
BASE_PROMPT = """
You are a senior Professor of Hindi Literature and former Dean of Arts at Presidency University, Kolkata.
You specialise in Modern & Contemporary Hindi Poetry, Comparative Literature, Religion & Society in Hindi Texts,
and literary research guidance.
Write in a calm, reflective, academic tone.
When you answer:
- Give conceptual explanations, not bullet points.
- Quote Hindi authors or critics where appropriate.
- Encourage comparative analysis.
- Use Devanagari for quotes and titles; English for explanation unless full Hindi mode is selected.
"""
# ------------------------------------------------------------
# Dropdown Options
# ------------------------------------------------------------
DOMAINS = [
"Modern Hindi Poetry (आधुनिक हिंदी कविता)",
"Contemporary Hindi Poetry (समकालीन कविता)",
"Medieval Hindi Literature (मध्यकालीन हिंदी साहित्य)",
"Modern Hindi Fiction & Prose (आधुनिक गद्य)",
"Comparative Hindi Literature (तुलनात्मक साहित्य)",
"Religion & Philosophy in Hindi Literature (धर्म और दर्शन)",
"Research Methodology in Hindi Studies (अनुसंधान पद्धति)",
"Translation Studies (अनुवाद अध्ययन)",
"Dalit & Feminist Literature (दलित और नारीवादी साहित्य)",
"Postmodern and Global Hindi (उत्तर आधुनिकता और वैश्विक हिंदी)",
]
AUDIENCES = [
"Undergraduate Student",
"Postgraduate Student",
"PhD / Research Scholar",
"Faculty / Teacher",
"Independent Reader",
]
# ------------------------------------------------------------
# LLM Response
# ------------------------------------------------------------
def generate_response(domain, query, audience, language):
lang_note = (
"Respond in English with Devanagari quotes."
if language == "English"
else "Respond fully in Hindi (Devanagari)."
)
prompt = (
f"{BASE_PROMPT}\n\nDomain: {domain}\nAudience: {audience}\n{lang_note}\n\n"
f"Student Query:\n{query}\n\nAnswer:"
)
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "system", "content": prompt}],
temperature=0.7,
max_tokens=800,
)
return response["choices"][0]["message"]["content"]
except Exception as e:
return f"⚠️ Error fetching response: {e}"
# ------------------------------------------------------------
# Knowledge Base (Example Notes)
# ------------------------------------------------------------
KNOWLEDGE_BASE = {
"Research Guidance": [
"How to select a topic for Hindi PhD.",
"Common pitfalls in literary analysis.",
"Structuring dissertation chapters.",
"Citation and referencing in Hindi research.",
],
"Comparative Literature": [
"Hindi–Bengali modernist influences.",
"Urdu–Hindi poetic dialogue.",
"Translation as interpretation.",
"Cross-cultural motifs in Hindi fiction.",
],
"Commentaries": [
"Annotated readings of Agyeya, Nirala, and Muktibodh.",
"Bhakti and Modernity: Kabir to Nagarjun.",
"Gender in Hindi poetry: Mahadevi Verma and beyond.",
],
}
# ------------------------------------------------------------
# Interface Logic
# ------------------------------------------------------------
def mentorship_interface(domain, audience, query, language):
return generate_response(domain, query, audience, language)
# ------------------------------------------------------------
# Gradio Interface
# ------------------------------------------------------------
with gr.Blocks(title=APP_TITLE) as demo:
gr.Markdown(f"## 🌸 {APP_TITLE}")
gr.Markdown(APP_DESCRIPTION)
with gr.Tab("A. Study Modules & Guidance"):
domain = gr.Dropdown(choices=DOMAINS, label="Select Literary Domain")
audience = gr.Dropdown(choices=AUDIENCES, label="Audience Type")
query = gr.Textbox(label="Enter your academic query", lines=4)
language = gr.Radio(["English", "Hindi"], label="Response Language", value="English")
output = gr.Textbox(label="Professor’s Response", lines=12)
ask_btn = gr.Button("Ask for Guidance")
ask_btn.click(mentorship_interface, [domain, audience, query, language], output)
with gr.Tab("B. Research Mentorship"):
gr.Markdown("💡 Detailed mentoring on research design and methodology:")
for topic in KNOWLEDGE_BASE["Research Guidance"]:
gr.Markdown(f"- {topic}")
with gr.Tab("C. Comparative Literature Lab"):
gr.Markdown("🔶 Intersections of Hindi with other literatures:")
for topic in KNOWLEDGE_BASE["Comparative Literature"]:
gr.Markdown(f"- {topic}")
with gr.Tab("D. Annotated Texts & Commentaries"):
gr.Markdown("📘 Selected commentaries and readings:")
for topic in KNOWLEDGE_BASE["Commentaries"]:
gr.Markdown(f"- {topic}")
with gr.Tab("E. Recorded Intellectual Presence"):
gr.Markdown("🎙️ Placeholder for lectures and reflections.")
gr.Markdown("_Coming soon: Audio-visual archives of the Professor._")
with gr.Tab("F. Ask the Professor"):
gr.Markdown("✍️ Submit questions for asynchronous academic discussion.")
question = gr.Textbox(label="Your Question", lines=3)
reply = gr.Textbox(label="Response", lines=8)
submit_btn = gr.Button("Submit Question")
submit_btn.click(mentorship_interface, [domain, audience, question, language], reply)
with gr.Tab("Audience & Contribution"):
gr.Markdown(
"👥 Former students and scholars may contribute annotated notes, "
"comparative findings, and new research topics."
)
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)