prasai-ap commited on
Commit
49bb88f
·
verified ·
1 Parent(s): 671e12d

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -3
app.py CHANGED
@@ -30,7 +30,7 @@ def ask_tutor(question: str, textbook_context: str) -> tuple[str, str, str]:
30
  if BACKEND_URL:
31
  backend_result = ask_backend(question)
32
 
33
- if backend_result:
34
  return backend_result
35
 
36
  return mock_response(question=question, textbook_context=textbook_context)
@@ -72,6 +72,18 @@ def format_backend_response(data: dict[str, Any]) -> tuple[str, str, str]:
72
  )
73
 
74
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  def mock_response(question: str, textbook_context: str) -> tuple[str, str, str]:
76
  context = textbook_context or EXAMPLE_CONTEXT
77
  simple_context = truncate(context, max_length=450)
@@ -84,8 +96,7 @@ def mock_response(question: str, textbook_context: str) -> tuple[str, str, str]:
84
  )
85
  nepali = (
86
  "मक डेमो उत्तर: म पाठ्यपुस्तकको सन्दर्भ मात्र प्रयोग गर्दैछु। "
87
- "यो विषयलाई सरल रूपमा बुझ्दा, माटोको माथिल्लो उपयोगी भाग हावा वा पानीले "
88
- "बगाएर लैजान सक्छ। यस्तो हुँदा खेतीका लागि माटो कमजोर हुन सक्छ।"
89
  )
90
  quiz = format_quiz(
91
  [
@@ -98,6 +109,38 @@ def mock_response(question: str, textbook_context: str) -> tuple[str, str, str]:
98
  return english, nepali, quiz
99
 
100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  def normalize_question_mock(question: str) -> str:
102
  text = question.lower()
103
 
 
30
  if BACKEND_URL:
31
  backend_result = ask_backend(question)
32
 
33
+ if backend_result and not is_insufficient_backend_result(backend_result):
34
  return backend_result
35
 
36
  return mock_response(question=question, textbook_context=textbook_context)
 
72
  )
73
 
74
 
75
+ def is_insufficient_backend_result(result: tuple[str, str, str]) -> bool:
76
+ combined = " ".join(result).lower()
77
+ markers = [
78
+ "not have enough textbook context",
79
+ "not enough textbook context",
80
+ "insufficient context",
81
+ "पर्याप्त जानकारी छैन",
82
+ "पर्याप्त सन्दर्भ छैन",
83
+ ]
84
+ return any(marker in combined for marker in markers)
85
+
86
+
87
  def mock_response(question: str, textbook_context: str) -> tuple[str, str, str]:
88
  context = textbook_context or EXAMPLE_CONTEXT
89
  simple_context = truncate(context, max_length=450)
 
96
  )
97
  nepali = (
98
  "मक डेमो उत्तर: म पाठ्यपुस्तकको सन्दर्भ मात्र प्रयोग गर्दैछु। "
99
+ f"{mock_nepali_explanation(normalized_question)}"
 
100
  )
101
  quiz = format_quiz(
102
  [
 
109
  return english, nepali, quiz
110
 
111
 
112
+ def mock_nepali_explanation(normalized_question: str) -> str:
113
+ text = normalized_question.lower()
114
+
115
+ if "soil erosion" in text:
116
+ return (
117
+ "माटो कटान भनेको हावा, पानी वा अरू प्राकृतिक कारणले माटोको माथिल्लो "
118
+ "मलिलो भाग हट्नु हो। यसले खेतको माटो कमजोर बनाउन सक्छ। रूख र घाँस "
119
+ "लगाउँदा माटो जोगाउन मद्दत हुन्छ।"
120
+ )
121
+
122
+ if "photosynthesis" in text:
123
+ return (
124
+ "प्रकाश संश्लेषण भनेको हरिया बिरुवाले घामको प्रकाश, पानी र कार्बन "
125
+ "डाइअक्साइड प्रयोग गरेर आफ्नो खाना बनाउने प्रक्रिया हो। यस क्रममा "
126
+ "अक्सिजन पनि निस्कन्छ।"
127
+ )
128
+
129
+ if "fraction" in text:
130
+ return (
131
+ "भिन्न भनेको कुनै पूर्ण वस्तुको भाग देखाउने संख्या हो। जस्तै, एउटा "
132
+ "रोटी बराबर भागमा काट्दा एक भागलाई भिन्नबाट देखाउन सकिन्छ।"
133
+ )
134
+
135
+ if "oxygen" in text:
136
+ return (
137
+ "अक्सिजन एउटा ग्यास हो। मानिस, जनावर र धेरै जीवहरूले सास फेर्दा "
138
+ "अक्सिजन प्रयोग गर्छन्। यो जीवनका लागि महत्त्वपूर्ण हुन्छ।"
139
+ )
140
+
141
+ return "यो विषयलाई सरल रूपमा बुझ्न पाठ्यपुस्तकको सन्दर्भ पढेर मुख्य कुरा सम्झनुहोस्।"
142
+
143
+
144
  def normalize_question_mock(question: str) -> str:
145
  text = question.lower()
146