ldrissi commited on
Commit
33c2628
·
1 Parent(s): 3aabd8c

finish the idea of micro learning part 5

Browse files
Files changed (1) hide show
  1. app.py +24 -120
app.py CHANGED
@@ -1,127 +1,31 @@
1
- # Improved app.py with real summarization using Hugging Face model
2
  import gradio as gr
3
- from transformers import pipeline
4
 
5
- # Initialize the summarization model
6
- try:
7
- # Use a smaller, faster model for summarization
8
- summarizer = pipeline("summarization", model="facebook/bart-large-cnn", device=-1) # Using CPU
9
- print("Summarization model loaded successfully!")
10
- except Exception as e:
11
- print(f"Error loading summarization model: {e}")
12
- summarizer = None
13
 
14
- # Mock data for demonstration
15
- sample_content = {
16
- "module1": {
17
- "title": "Introduction to AI",
18
- "text": "Artificial Intelligence (AI) is the simulation of human intelligence processes by machines, especially computer systems. These processes include learning, reasoning, and self-correction. AI can be categorized as either weak or strong. Weak AI is designed to complete a narrow task, like facial recognition. Strong AI can perform any intellectual task that a human being can do."
19
- },
20
- "module2": {
21
- "title": "Python Basics",
22
- "text": "Python is a high-level, interpreted programming language known for its readability and simplicity. It is widely used in data science, machine learning, and web development. Python's syntax allows programmers to express concepts in fewer lines of code than languages like C++ or Java. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming."
23
- },
24
- "module3": {
25
- "title": "Microlearning Concepts",
26
- "text": "Microlearning is an educational strategy that focuses on delivering content in small, specific bursts. Each learning unit typically addresses one learning objective and takes 3-5 minutes to complete. This approach is particularly effective for modern learners with limited time and attention spans. It often incorporates multimedia elements and is optimized for mobile devices, allowing learning to happen anywhere."
27
- }
28
- }
29
 
30
- # Function to generate real summaries using Hugging Face model
31
- def get_summary(content_id):
32
- """Generate a summary using a HuggingFace model"""
33
- if content_id not in sample_content:
34
- return "Content not found"
35
-
36
- text = sample_content[content_id]["text"]
37
-
38
- if summarizer is None:
39
- # Fallback to simple summary if model failed to load
40
- return text.split('.')[0] + "."
41
-
42
- try:
43
- # Generate summary using the model
44
- summary = summarizer(text, max_length=60, min_length=20, do_sample=False)
45
- return summary[0]['summary_text']
46
- except Exception as e:
47
- print(f"Error generating summary: {e}")
48
- # Fallback to simple summary if model fails
49
- return f"Error generating summary. Fallback: {text.split('.')[0]}."
50
 
51
- def answer_question(content_id, question):
52
- """Generate a mock answer for demonstration"""
53
- if content_id not in sample_content:
54
- return "Content not found"
55
-
56
- text = sample_content[content_id]["text"]
57
-
58
- # Simple mock QA system
59
- question_words = set(question.lower().split())
60
- important_words = [word for word in question_words if len(word) > 3 and word not in ["what", "when", "where", "which", "how", "this", "that", "with", "from", "have", "about"]]
61
-
62
- if any(word in text.lower() for word in important_words):
63
- return f"Based on the content, I can tell you that the answer relates to {sample_content[content_id]['title']}."
64
- else:
65
- return "I don't have enough information to answer that question based on the selected content."
66
-
67
- # Create the Gradio interface
68
- demo = gr.Blocks(title="Micro Learning Platform")
69
-
70
- with demo:
71
- gr.Markdown("# Micro Learning Platform")
72
- gr.Markdown("## A demonstration of microlearning concepts with AI")
73
-
74
- with gr.Tab("Browse Content"):
75
- content_dropdown = gr.Dropdown(
76
- choices=[{"value": k, "label": v["title"]} for k, v in sample_content.items()],
77
- value="module1",
78
- label="Select a learning module"
79
- )
80
-
81
- content_display = gr.Textbox(
82
- value=sample_content["module1"]["text"],
83
- label="Module content",
84
- lines=5,
85
- interactive=False
86
- )
87
-
88
- def update_content(module_id):
89
- return sample_content.get(module_id, {"text": "Content not found"})["text"]
90
-
91
- content_dropdown.change(
92
- fn=update_content,
93
- inputs=content_dropdown,
94
- outputs=content_display
95
- )
96
-
97
- with gr.Tab("Study Tools"):
98
- with gr.Row():
99
- study_content_dropdown = gr.Dropdown(
100
- choices=[{"value": k, "label": v["title"]} for k, v in sample_content.items()],
101
- value="module1",
102
- label="Select a learning module"
103
- )
104
-
105
- with gr.Tab("Summarize"):
106
- summary_button = gr.Button("Generate Summary")
107
- summary_output = gr.Textbox(label="Summary", lines=3)
108
-
109
- summary_button.click(
110
- fn=get_summary,
111
- inputs=study_content_dropdown,
112
- outputs=summary_output
113
- )
114
-
115
- with gr.Tab("Ask Question"):
116
- question_input = gr.Textbox(label="Your Question", placeholder="Type your question here...")
117
- ask_button = gr.Button("Submit Question")
118
- answer_output = gr.Textbox(label="Answer", lines=3)
119
-
120
- ask_button.click(
121
- fn=answer_question,
122
- inputs=[study_content_dropdown, question_input],
123
- outputs=answer_output
124
- )
125
 
126
  # Launch the application
127
- demo.launch()
 
 
 
1
+ # Super minimal app.py for troubleshooting
2
  import gradio as gr
3
+ import sys
4
 
5
+ # Print Python version and loaded modules for debugging
6
+ print(f"Python version: {sys.version}")
7
+ print("Loaded modules:")
8
+ for name, module in sys.modules.items():
9
+ if hasattr(module, "__version__"):
10
+ print(f" {name}: {module.__version__}")
 
 
11
 
12
+ # Simple function that doesn't require any ML models
13
+ def echo(text):
14
+ return f"You said: {text}"
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
+ # Create a simple Gradio interface
17
+ demo = gr.Interface(
18
+ fn=echo,
19
+ inputs=gr.Textbox(placeholder="Type something here..."),
20
+ outputs=gr.Textbox(),
21
+ title="Micro Learning Platform - Debug Version",
22
+ description="This is a minimal version for troubleshooting"
23
+ )
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
+ # Add a clear print statement when the app starts
26
+ print("Application starting...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  # Launch the application
29
+ if __name__ == "__main__":
30
+ demo.launch()
31
+ print("Application launched successfully!")