Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,7 +14,7 @@ APP_DESCRIPTION = (
|
|
| 14 |
)
|
| 15 |
|
| 16 |
# ------------------------------------------------------------
|
| 17 |
-
# API Key
|
| 18 |
# ------------------------------------------------------------
|
| 19 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 20 |
|
|
@@ -26,16 +26,16 @@ You are a senior Professor of Hindi Literature and former Dean of Arts at Presid
|
|
| 26 |
You specialise in Modern & Contemporary Hindi Poetry, Comparative Literature, Religion & Society in Hindi Texts,
|
| 27 |
and literary research guidance.
|
| 28 |
|
| 29 |
-
Write in a
|
| 30 |
-
When
|
| 31 |
-
- Give conceptual explanations
|
| 32 |
-
- Quote
|
| 33 |
-
- Encourage comparative
|
| 34 |
-
- Use Devanagari for
|
| 35 |
"""
|
| 36 |
|
| 37 |
# ------------------------------------------------------------
|
| 38 |
-
#
|
| 39 |
# ------------------------------------------------------------
|
| 40 |
DOMAINS = [
|
| 41 |
"Modern Hindi Poetry (आधुनिक हिंदी कविता)",
|
|
@@ -59,113 +59,104 @@ AUDIENCES = [
|
|
| 59 |
]
|
| 60 |
|
| 61 |
# ------------------------------------------------------------
|
| 62 |
-
# LLM Response
|
| 63 |
# ------------------------------------------------------------
|
| 64 |
def generate_response(domain, query, audience, language):
|
| 65 |
lang_note = (
|
| 66 |
"Respond in English with Devanagari quotes."
|
| 67 |
if language == "English"
|
| 68 |
-
else "Respond
|
| 69 |
)
|
| 70 |
prompt = (
|
| 71 |
f"{BASE_PROMPT}\n\nDomain: {domain}\nAudience: {audience}\n{lang_note}\n\n"
|
| 72 |
f"Student Query:\n{query}\n\nAnswer:"
|
| 73 |
)
|
| 74 |
try:
|
| 75 |
-
|
| 76 |
-
model="gpt-
|
| 77 |
messages=[{"role": "system", "content": prompt}],
|
| 78 |
temperature=0.7,
|
| 79 |
-
max_tokens=
|
| 80 |
)
|
| 81 |
-
return
|
| 82 |
except Exception as e:
|
| 83 |
return f"⚠️ Error fetching response: {e}"
|
| 84 |
|
| 85 |
# ------------------------------------------------------------
|
| 86 |
-
# Knowledge Base
|
| 87 |
# ------------------------------------------------------------
|
| 88 |
KNOWLEDGE_BASE = {
|
| 89 |
"Research Guidance": [
|
| 90 |
-
"How to select a
|
| 91 |
"Common pitfalls in literary analysis.",
|
| 92 |
"Structuring dissertation chapters.",
|
| 93 |
-
"Citation
|
| 94 |
],
|
| 95 |
"Comparative Literature": [
|
| 96 |
"Hindi–Bengali modernist influences.",
|
| 97 |
"Urdu–Hindi poetic dialogue.",
|
| 98 |
"Translation as interpretation.",
|
| 99 |
-
"Cross-cultural motifs in
|
| 100 |
],
|
| 101 |
"Commentaries": [
|
| 102 |
-
"Annotated readings of Agyeya, Nirala and Muktibodh.",
|
| 103 |
"Bhakti and Modernity: Kabir to Nagarjun.",
|
| 104 |
"Gender in Hindi poetry: Mahadevi Verma and beyond.",
|
| 105 |
],
|
| 106 |
}
|
| 107 |
|
| 108 |
# ------------------------------------------------------------
|
| 109 |
-
# Interface
|
| 110 |
# ------------------------------------------------------------
|
| 111 |
def mentorship_interface(domain, audience, query, language):
|
| 112 |
return generate_response(domain, query, audience, language)
|
| 113 |
|
| 114 |
# ------------------------------------------------------------
|
| 115 |
-
#
|
| 116 |
# ------------------------------------------------------------
|
| 117 |
-
with gr.Blocks(title=APP_TITLE
|
| 118 |
-
gr.Markdown(f"# 🌸 {APP_TITLE}")
|
| 119 |
gr.Markdown(APP_DESCRIPTION)
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
query = gr.TextArea(label="Enter your academic query", lines=4)
|
| 126 |
language = gr.Radio(["English", "Hindi"], label="Response Language", value="English")
|
| 127 |
-
output = gr.
|
| 128 |
-
gr.Button("Ask for Guidance")
|
| 129 |
-
|
| 130 |
-
)
|
| 131 |
|
| 132 |
-
|
| 133 |
-
with gr.Tab("B · Research Mentorship"):
|
| 134 |
gr.Markdown("💡 Detailed mentoring on research design and methodology:")
|
| 135 |
-
for
|
| 136 |
-
gr.Markdown(f"- {
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
gr.Markdown("
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
question = gr.TextArea(label="Your Question", lines=3)
|
| 159 |
-
response = gr.TextArea(label="Response Area", lines=8)
|
| 160 |
-
gr.Button("Submit Question").click(
|
| 161 |
-
mentorship_interface, [domain, audience, question, language], response
|
| 162 |
-
)
|
| 163 |
|
| 164 |
-
# ---------- G ----------
|
| 165 |
with gr.Tab("Audience & Contribution"):
|
| 166 |
gr.Markdown(
|
| 167 |
-
"👥 Former students and scholars may contribute annotated notes
|
| 168 |
-
"
|
| 169 |
)
|
| 170 |
|
| 171 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
|
| 14 |
)
|
| 15 |
|
| 16 |
# ------------------------------------------------------------
|
| 17 |
+
# OpenAI API Key Setup
|
| 18 |
# ------------------------------------------------------------
|
| 19 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 20 |
|
|
|
|
| 26 |
You specialise in Modern & Contemporary Hindi Poetry, Comparative Literature, Religion & Society in Hindi Texts,
|
| 27 |
and literary research guidance.
|
| 28 |
|
| 29 |
+
Write in a calm, reflective, academic tone.
|
| 30 |
+
When you answer:
|
| 31 |
+
- Give conceptual explanations, not bullet points.
|
| 32 |
+
- Quote Hindi authors or critics where appropriate.
|
| 33 |
+
- Encourage comparative analysis.
|
| 34 |
+
- Use Devanagari for quotes and titles; English for explanation unless full Hindi mode is selected.
|
| 35 |
"""
|
| 36 |
|
| 37 |
# ------------------------------------------------------------
|
| 38 |
+
# Dropdown Options
|
| 39 |
# ------------------------------------------------------------
|
| 40 |
DOMAINS = [
|
| 41 |
"Modern Hindi Poetry (आधुनिक हिंदी कविता)",
|
|
|
|
| 59 |
]
|
| 60 |
|
| 61 |
# ------------------------------------------------------------
|
| 62 |
+
# LLM Response
|
| 63 |
# ------------------------------------------------------------
|
| 64 |
def generate_response(domain, query, audience, language):
|
| 65 |
lang_note = (
|
| 66 |
"Respond in English with Devanagari quotes."
|
| 67 |
if language == "English"
|
| 68 |
+
else "Respond fully in Hindi (Devanagari)."
|
| 69 |
)
|
| 70 |
prompt = (
|
| 71 |
f"{BASE_PROMPT}\n\nDomain: {domain}\nAudience: {audience}\n{lang_note}\n\n"
|
| 72 |
f"Student Query:\n{query}\n\nAnswer:"
|
| 73 |
)
|
| 74 |
try:
|
| 75 |
+
response = openai.ChatCompletion.create(
|
| 76 |
+
model="gpt-3.5-turbo",
|
| 77 |
messages=[{"role": "system", "content": prompt}],
|
| 78 |
temperature=0.7,
|
| 79 |
+
max_tokens=800,
|
| 80 |
)
|
| 81 |
+
return response["choices"][0]["message"]["content"]
|
| 82 |
except Exception as e:
|
| 83 |
return f"⚠️ Error fetching response: {e}"
|
| 84 |
|
| 85 |
# ------------------------------------------------------------
|
| 86 |
+
# Knowledge Base (Example Notes)
|
| 87 |
# ------------------------------------------------------------
|
| 88 |
KNOWLEDGE_BASE = {
|
| 89 |
"Research Guidance": [
|
| 90 |
+
"How to select a topic for Hindi PhD.",
|
| 91 |
"Common pitfalls in literary analysis.",
|
| 92 |
"Structuring dissertation chapters.",
|
| 93 |
+
"Citation and referencing in Hindi research.",
|
| 94 |
],
|
| 95 |
"Comparative Literature": [
|
| 96 |
"Hindi–Bengali modernist influences.",
|
| 97 |
"Urdu–Hindi poetic dialogue.",
|
| 98 |
"Translation as interpretation.",
|
| 99 |
+
"Cross-cultural motifs in Hindi fiction.",
|
| 100 |
],
|
| 101 |
"Commentaries": [
|
| 102 |
+
"Annotated readings of Agyeya, Nirala, and Muktibodh.",
|
| 103 |
"Bhakti and Modernity: Kabir to Nagarjun.",
|
| 104 |
"Gender in Hindi poetry: Mahadevi Verma and beyond.",
|
| 105 |
],
|
| 106 |
}
|
| 107 |
|
| 108 |
# ------------------------------------------------------------
|
| 109 |
+
# Interface Logic
|
| 110 |
# ------------------------------------------------------------
|
| 111 |
def mentorship_interface(domain, audience, query, language):
|
| 112 |
return generate_response(domain, query, audience, language)
|
| 113 |
|
| 114 |
# ------------------------------------------------------------
|
| 115 |
+
# Gradio Interface
|
| 116 |
# ------------------------------------------------------------
|
| 117 |
+
with gr.Blocks(title=APP_TITLE) as demo:
|
| 118 |
+
gr.Markdown(f"## 🌸 {APP_TITLE}")
|
| 119 |
gr.Markdown(APP_DESCRIPTION)
|
| 120 |
|
| 121 |
+
with gr.Tab("A. Study Modules & Guidance"):
|
| 122 |
+
domain = gr.Dropdown(choices=DOMAINS, label="Select Literary Domain")
|
| 123 |
+
audience = gr.Dropdown(choices=AUDIENCES, label="Audience Type")
|
| 124 |
+
query = gr.Textbox(label="Enter your academic query", lines=4)
|
|
|
|
| 125 |
language = gr.Radio(["English", "Hindi"], label="Response Language", value="English")
|
| 126 |
+
output = gr.Textbox(label="Professor’s Response", lines=12)
|
| 127 |
+
ask_btn = gr.Button("Ask for Guidance")
|
| 128 |
+
ask_btn.click(mentorship_interface, [domain, audience, query, language], output)
|
|
|
|
| 129 |
|
| 130 |
+
with gr.Tab("B. Research Mentorship"):
|
|
|
|
| 131 |
gr.Markdown("💡 Detailed mentoring on research design and methodology:")
|
| 132 |
+
for topic in KNOWLEDGE_BASE["Research Guidance"]:
|
| 133 |
+
gr.Markdown(f"- {topic}")
|
| 134 |
+
|
| 135 |
+
with gr.Tab("C. Comparative Literature Lab"):
|
| 136 |
+
gr.Markdown("🔶 Intersections of Hindi with other literatures:")
|
| 137 |
+
for topic in KNOWLEDGE_BASE["Comparative Literature"]:
|
| 138 |
+
gr.Markdown(f"- {topic}")
|
| 139 |
+
|
| 140 |
+
with gr.Tab("D. Annotated Texts & Commentaries"):
|
| 141 |
+
gr.Markdown("📘 Selected commentaries and readings:")
|
| 142 |
+
for topic in KNOWLEDGE_BASE["Commentaries"]:
|
| 143 |
+
gr.Markdown(f"- {topic}")
|
| 144 |
+
|
| 145 |
+
with gr.Tab("E. Recorded Intellectual Presence"):
|
| 146 |
+
gr.Markdown("🎙️ Placeholder for lectures and reflections.")
|
| 147 |
+
gr.Markdown("_Coming soon: Audio-visual archives of the Professor._")
|
| 148 |
+
|
| 149 |
+
with gr.Tab("F. Ask the Professor"):
|
| 150 |
+
gr.Markdown("✍️ Submit questions for asynchronous academic discussion.")
|
| 151 |
+
question = gr.Textbox(label="Your Question", lines=3)
|
| 152 |
+
reply = gr.Textbox(label="Response", lines=8)
|
| 153 |
+
submit_btn = gr.Button("Submit Question")
|
| 154 |
+
submit_btn.click(mentorship_interface, [domain, audience, question, language], reply)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
|
|
|
| 156 |
with gr.Tab("Audience & Contribution"):
|
| 157 |
gr.Markdown(
|
| 158 |
+
"👥 Former students and scholars may contribute annotated notes, "
|
| 159 |
+
"comparative findings, and new research topics."
|
| 160 |
)
|
| 161 |
|
| 162 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|