Spaces:
Sleeping
Sleeping
Commit
·
41bb926
1
Parent(s):
8dec9db
✅ Final: Updated app.py and cleaned requirements.txt
Browse files- app.py +33 -25
- 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 |
-
|
|
|
|
| 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": "
|
| 118 |
-
"explanation": "
|
| 119 |
-
"comparison": "
|
| 120 |
-
"list": "
|
| 121 |
-
"general": "
|
| 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
|
| 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 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
Upload your lecture
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|