Spaces:
Sleeping
Sleeping
| import os | |
| import openai | |
| import gradio as gr | |
| from datetime import datetime | |
| from docx import Document | |
| from docx.shared import Pt | |
| from docx.enum.text import WD_PARAGRAPH_ALIGNMENT | |
| # ------------------------------------------------------------ | |
| # 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 str(language).lower().startswith("eng") | |
| 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.8, | |
| max_tokens=900, | |
| ) | |
| return response["choices"][0]["message"]["content"] | |
| except Exception as e: | |
| return f"⚠️ Error fetching response: {e}" | |
| # ------------------------------------------------------------ | |
| # Save DOCX with Professor Letterhead | |
| # ------------------------------------------------------------ | |
| def save_docx_with_letterhead(report_text): | |
| os.makedirs("reports", exist_ok=True) | |
| fname = f"reports/Hindi_Report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.docx" | |
| doc = Document() | |
| # ------------------ Letterhead ------------------ | |
| header = doc.add_paragraph() | |
| header.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER | |
| run = header.add_run( | |
| "Prof. Tanuja Majumdar\nEx-Dean & HOD, Department of Arts\nPresidency University, Kolkata" | |
| ) | |
| run.bold = True | |
| run.font.size = Pt(14) | |
| doc.add_paragraph("\n") # spacing | |
| # ------------------ Report Title ------------------ | |
| heading = doc.add_paragraph() | |
| heading.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER | |
| run = heading.add_run("Parampara & Prayog — Comprehensive Hindi Literature Report") | |
| run.bold = True | |
| run.font.size = Pt(16) | |
| doc.add_paragraph("\n") # spacing | |
| # ------------------ Date ------------------ | |
| date_para = doc.add_paragraph() | |
| date_para.alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT | |
| run = date_para.add_run(f"Date: {datetime.now().strftime('%d %B, %Y')}") | |
| run.italic = True | |
| run.font.size = Pt(10) | |
| doc.add_paragraph("\n") # spacing | |
| # ------------------ Content ------------------ | |
| # If report_text contains headings for sections, we can bold them | |
| for line in report_text.split("\n"): | |
| p = doc.add_paragraph() | |
| if line.strip().startswith("## "): # markdown-style heading | |
| run = p.add_run(line.replace("## ", "")) | |
| run.bold = True | |
| run.font.size = Pt(12) | |
| else: | |
| run = p.add_run(line) | |
| run.font.size = Pt(11) | |
| doc.save(fname) | |
| return fname | |
| # ------------------------------------------------------------ | |
| # 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.", | |
| ], | |
| } | |
| # ------------------------------------------------------------ | |
| # Gradio Interface | |
| # ------------------------------------------------------------ | |
| def mentorship_interface(domain, audience, query, language): | |
| return generate_response(domain, query, audience, language) | |
| with gr.Blocks(title=APP_TITLE) as demo: | |
| gr.Markdown(f"## 🌸 {APP_TITLE}") | |
| gr.Markdown(APP_DESCRIPTION) | |
| # ---------------- Tab A ---------------- | |
| with gr.Tab("A. Study Modules & Guidance"): | |
| domain_a = gr.Dropdown(choices=DOMAINS, label="Select Literary Domain") | |
| audience_a = gr.Dropdown(choices=AUDIENCES, label="Audience Type") | |
| query_a = gr.Textbox(label="Enter your academic query", lines=4) | |
| language_a = gr.Radio(["English", "Hindi"], label="Response Language", value="English") | |
| output_a = gr.Textbox(label="Professor’s Response", lines=12) | |
| ask_btn_a = gr.Button("Ask for Guidance") | |
| ask_btn_a.click(mentorship_interface, [domain_a, audience_a, query_a, language_a], output_a) | |
| # ------------------ Download Button ------------------ | |
| download_btn_a = gr.Button("Download as DOCX") | |
| download_file_a = gr.File(label="Download Report") | |
| # Function returns the file path for direct download | |
| def download_docx_direct(text): | |
| path = save_docx_with_letterhead(text) | |
| return path | |
| download_btn_a.click(download_docx_direct, inputs=[output_a], outputs=[download_file_a]) | |
| # ---------------- Tab B ---------------- | |
| with gr.Tab("B. Research Mentorship"): | |
| domain_b = gr.Dropdown(choices=DOMAINS, label="Select Literary Domain") | |
| audience_b = gr.Dropdown(choices=AUDIENCES, label="Audience Type") | |
| question_b = gr.Textbox(label="Ask about Research Design", lines=3) | |
| answer_b = gr.Textbox(label="Response", lines=8) | |
| btn_b = gr.Button("Get Mentorship Advice") | |
| language_b = gr.Radio(["English", "Hindi"], label="Response Language", value="English") | |
| btn_b.click(mentorship_interface, [domain_b, audience_b, question_b, language_b], answer_b) | |
| # ---------------- Tab C ---------------- | |
| with gr.Tab("C. Comparative Literature Lab"): | |
| domain_c = gr.Dropdown(choices=DOMAINS, label="Select Literary Domain") | |
| audience_c = gr.Dropdown(choices=AUDIENCES, label="Audience Type") | |
| question_c = gr.Textbox(label="Ask about Comparative Literature", lines=3) | |
| answer_c = gr.Textbox(label="Response", lines=8) | |
| btn_c = gr.Button("Get Comparative Insight") | |
| language_c = gr.Radio(["English", "Hindi"], label="Response Language", value="English") | |
| btn_c.click(mentorship_interface, [domain_c, audience_c, question_c, language_c], answer_c) | |
| # ---------------- Tab D ---------------- | |
| with gr.Tab("D. Annotated Texts & Commentaries"): | |
| domain_d = gr.Dropdown(choices=DOMAINS, label="Select Literary Domain") | |
| audience_d = gr.Dropdown(choices=AUDIENCES, label="Audience Type") | |
| question_d = gr.Textbox(label="Ask about Commentary / Text", lines=3) | |
| answer_d = gr.Textbox(label="Response", lines=8) | |
| btn_d = gr.Button("Get Commentary Insight") | |
| language_d = gr.Radio(["English", "Hindi"], label="Response Language", value="English") | |
| btn_d.click(mentorship_interface, [domain_d, audience_d, question_d, language_d], answer_d) | |
| # ---------------- Tab E ---------------- | |
| with gr.Tab("E. Recorded Intellectual Presence"): | |
| gr.Markdown("🎙️ Placeholder for lectures and reflections.") | |
| gr.Markdown("_Coming soon: Audio-visual archives of the Professor._") | |
| # ---------------- Tab F ---------------- | |
| with gr.Tab("F. Ask the Professor"): | |
| domain_f = gr.Dropdown(choices=DOMAINS, label="Select Literary Domain") | |
| audience_f = gr.Dropdown(choices=AUDIENCES, label="Audience Type") | |
| question_f = gr.Textbox(label="Your Question", lines=3) | |
| reply_f = gr.Textbox(label="Response", lines=8) | |
| submit_btn_f = gr.Button("Submit Question") | |
| language_f = gr.Radio(["English", "Hindi"], label="Response Language", value="English") | |
| submit_btn_f.click(mentorship_interface, [domain_f, audience_f, question_f, language_f], reply_f) | |
| # ---------------- Tab G ---------------- | |
| with gr.Tab("Audience & Contribution"): | |
| gr.Markdown( | |
| "👥 Former students and scholars may contribute annotated notes, " | |
| "comparative findings, and new research topics." | |
| ) | |
| # ---------------- Tab H: DOCX Download ---------------- | |
| with gr.Tab("Download Report"): | |
| report_textbox = gr.Textbox(label="Enter text to save as DOCX", lines=15) | |
| save_doc_btn = gr.Button("Save Report as DOCX") | |
| save_output = gr.Textbox(label="Saved Report Path", lines=1) | |
| save_doc_btn.click(save_docx_with_letterhead, [report_textbox], save_output) | |
| demo.launch(server_name="0.0.0.0", server_port=7860) | |