umerforsure commited on
Commit
41bb926
·
1 Parent(s): 8dec9db

✅ Final: Updated app.py and cleaned requirements.txt

Browse files
Files changed (2) hide show
  1. app.py +33 -25
  2. requirements.txt +0 -1
app.py CHANGED
@@ -65,7 +65,8 @@ def process_file(file):
65
  try:
66
  ext = os.path.splitext(file.name)[1].lower()
67
  with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as tmp:
68
- tmp.write(file.read())
 
69
  tmp.flush()
70
  full_text = extract_text(tmp.name, ext)
71
 
@@ -114,17 +115,17 @@ def detect_question_type(q):
114
  def post_process_output(answer_text, question):
115
  qtype = detect_question_type(question)
116
  label_map = {
117
- "definition": "📘 **Definition**",
118
- "explanation": "📘 **Explanation**",
119
- "comparison": "📘 **Comparison**",
120
- "list": "📘 **Key Points**",
121
- "general": "📘 **Insight**",
122
  }
123
  answer_text = f"{label_map.get(qtype)}\n\n{answer_text}"
124
 
125
  if len(answer_text.split()) > 80:
126
  summary = summary_pipeline(answer_text, max_length=60, min_length=25, do_sample=False)[0]['summary_text']
127
- answer_text += f"\n\n📝 **Summary:** {summary.strip()}"
128
 
129
  return answer_text
130
 
@@ -150,24 +151,31 @@ def ask_question(question):
150
  return post_process_output(result.strip(), question)
151
 
152
  # Gradio UI
153
- title = "📚 AI Study Assistant"
154
- with gr.Blocks(css="footer {display:none !important}") as demo:
155
- gr.Markdown("""# 📘 AI Study Assistant
156
- Upload your lecture notes and ask deep academic questions. Powered by Phi-3 & FAISS.""")
157
-
158
- with gr.Row():
159
- file_input = gr.File(label="Upload Course Material (PDF, DOCX, TXT, PPTX)")
160
- upload_btn = gr.Button("Process File")
161
-
162
- status = gr.Textbox(label="Status", interactive=False)
163
-
164
- question = gr.Textbox(label="Ask a Question", placeholder="E.g., What is demand paging?")
165
- ask_btn = gr.Button("Get Answer")
166
- answer = gr.Markdown("", elem_id="answer-box")
167
-
168
- upload_btn.click(fn=process_file, inputs=file_input, outputs=status)
169
- ask_btn.click(fn=ask_question, inputs=question, outputs=answer)
 
 
 
 
 
 
 
 
170
 
171
  if __name__ == "__main__":
172
  demo.launch()
173
-
 
65
  try:
66
  ext = os.path.splitext(file.name)[1].lower()
67
  with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as tmp:
68
+ file_bytes = file.read() if hasattr(file, "read") else file
69
+ tmp.write(file_bytes)
70
  tmp.flush()
71
  full_text = extract_text(tmp.name, ext)
72
 
 
115
  def post_process_output(answer_text, question):
116
  qtype = detect_question_type(question)
117
  label_map = {
118
+ "definition": "\ud83d\udcd8 **Definition**",
119
+ "explanation": "\ud83d\udcd8 **Explanation**",
120
+ "comparison": "\ud83d\udcd8 **Comparison**",
121
+ "list": "\ud83d\udcd8 **Key Points**",
122
+ "general": "\ud83d\udcd8 **Insight**",
123
  }
124
  answer_text = f"{label_map.get(qtype)}\n\n{answer_text}"
125
 
126
  if len(answer_text.split()) > 80:
127
  summary = summary_pipeline(answer_text, max_length=60, min_length=25, do_sample=False)[0]['summary_text']
128
+ answer_text += f"\n\n\ud83d\udcdd **Summary:** {summary.strip()}"
129
 
130
  return answer_text
131
 
 
151
  return post_process_output(result.strip(), question)
152
 
153
  # Gradio UI
154
+ with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
155
+ gr.Markdown("""
156
+ # 📚 AI Study Assistant
157
+ Upload your lecture slide/text file, ask questions, and get intelligent answers powered by Phi-3.
158
+ """)
159
+
160
+ with gr.Tab("Upload & Ask"):
161
+ with gr.Row():
162
+ file_input = gr.File(label="📄 Upload File", file_types=[".pdf", ".docx", ".pptx", ".txt"])
163
+ upload_btn = gr.Button("Upload")
164
+ upload_output = gr.Textbox(label="Upload Status", interactive=False)
165
+ upload_btn.click(fn=process_file, inputs=file_input, outputs=upload_output)
166
+
167
+ gr.Markdown("---")
168
+
169
+ with gr.Row():
170
+ question = gr.Textbox(label="❓ Ask a question")
171
+ ask_btn = gr.Button("Ask")
172
+ answer = gr.Textbox(label="💡 Answer", interactive=False)
173
+ ask_btn.click(fn=ask_question, inputs=question, outputs=answer)
174
+
175
+ with gr.Tab("History"):
176
+ gr.Markdown("""
177
+ **⏳ Coming Soon**: Question-answer history, summarization view, more!
178
+ """)
179
 
180
  if __name__ == "__main__":
181
  demo.launch()
 
requirements.txt CHANGED
@@ -1,6 +1,5 @@
1
  transformers
2
  torch
3
- pypdf
4
  faiss-cpu
5
  python-pptx
6
  python-docx
 
1
  transformers
2
  torch
 
3
  faiss-cpu
4
  python-pptx
5
  python-docx