Militaryint commited on
Commit
ed5b451
·
verified ·
1 Parent(s): baac182

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -65
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 scholarly, reflective tone.
30
- When answering:
31
- - Give conceptual explanations rather than bullet points.
32
- - Quote relevant Hindi authors or critics where appropriate.
33
- - Encourage comparative and analytical thinking.
34
- - Use Devanagari for titles/quotes, English for discussion unless full-Hindi mode is chosen.
35
  """
36
 
37
  # ------------------------------------------------------------
38
- # Domain & Audience Dropdowns
39
  # ------------------------------------------------------------
40
  DOMAINS = [
41
  "Modern Hindi Poetry (आधुनिक हिंदी कविता)",
@@ -59,113 +59,104 @@ AUDIENCES = [
59
  ]
60
 
61
  # ------------------------------------------------------------
62
- # LLM Response Function
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 entirely in Hindi (Devanagari script)."
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
- resp = openai.ChatCompletion.create(
76
- model="gpt-4o-mini",
77
  messages=[{"role": "system", "content": prompt}],
78
  temperature=0.7,
79
- max_tokens=900,
80
  )
81
- return resp["choices"][0]["message"]["content"]
82
  except Exception as e:
83
  return f"⚠️ Error fetching response: {e}"
84
 
85
  # ------------------------------------------------------------
86
- # Knowledge Base Editable Examples
87
  # ------------------------------------------------------------
88
  KNOWLEDGE_BASE = {
89
  "Research Guidance": [
90
- "How to select a PhD topic in Hindi literature.",
91
  "Common pitfalls in literary analysis.",
92
  "Structuring dissertation chapters.",
93
- "Citation & 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 modern 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 Function
110
  # ------------------------------------------------------------
111
  def mentorship_interface(domain, audience, query, language):
112
  return generate_response(domain, query, audience, language)
113
 
114
  # ------------------------------------------------------------
115
- # Build Gradio App
116
  # ------------------------------------------------------------
117
- with gr.Blocks(title=APP_TITLE, theme=gr.themes.Soft()) as demo:
118
- gr.Markdown(f"# 🌸 {APP_TITLE}")
119
  gr.Markdown(APP_DESCRIPTION)
120
 
121
- # ---------- A ----------
122
- with gr.Tab("A · Study Modules & Guidance"):
123
- domain = gr.Dropdown(DOMAINS, label="Select Domain")
124
- audience = gr.Dropdown(AUDIENCES, label="Audience Type")
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.TextArea(label="Professor’s Response", lines=12)
128
- gr.Button("Ask for Guidance").click(
129
- mentorship_interface, [domain, audience, query, language], output
130
- )
131
 
132
- # ---------- B ----------
133
- with gr.Tab("B · Research Mentorship"):
134
  gr.Markdown("💡 Detailed mentoring on research design and methodology:")
135
- for i in KNOWLEDGE_BASE["Research Guidance"]:
136
- gr.Markdown(f"- {i}")
137
-
138
- # ---------- C ----------
139
- with gr.Tab("C · Comparative Literature Lab"):
140
- gr.Markdown("🔶 Explore intersections of Hindi with other literatures:")
141
- for i in KNOWLEDGE_BASE["Comparative Literature"]:
142
- gr.Markdown(f"- {i}")
143
-
144
- # ---------- D ----------
145
- with gr.Tab("D · Annotated Texts & Commentaries"):
146
- gr.Markdown("📘 Selected texts and professorial commentaries:")
147
- for i in KNOWLEDGE_BASE["Commentaries"]:
148
- gr.Markdown(f"- {i}")
149
-
150
- # ---------- E ----------
151
- with gr.Tab("E · Recorded Intellectual Presence"):
152
- gr.Markdown("🎙️ Placeholder for audio/video lectures and reflections.")
153
- gr.Markdown("_Coming soon archival recordings of the Professor._")
154
-
155
- # ---------- F ----------
156
- with gr.Tab("F · Ask the Professor"):
157
- gr.Markdown("✍️ Submit questions for asynchronous academic discussion:")
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 and comparative findings, "
168
- "building the ongoing digital *parampara* of Hindi studies."
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)