MuhammmadRizwanRizwan commited on
Commit
00c713d
·
verified ·
1 Parent(s): 08d97f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -64
app.py CHANGED
@@ -1,83 +1,83 @@
1
- # import gradio as gr
2
 
3
- # gr.load("models/dima806/facial_emotions_image_detection").launch()
4
 
5
- import gradio as gr
6
- import tensorflow as tf
7
- from transformers import pipeline
8
 
9
- def process_image(image):
10
- try:
11
- # Initialize emotion classifier
12
- classifier = pipeline("image-classification", model="dima806/facial_emotions_image_detection")
13
 
14
- # Get predictions
15
- result = classifier(image)
16
 
17
- # Convert results to required format
18
- emotions = {item['label']: float(item['score']) * 100 for item in result}
19
 
20
- # Generate analysis and report
21
- analysis = generate_analysis(emotions)
22
- report = generate_report(emotions, analysis)
23
 
24
- return [(k, v) for k, v in emotions.items()], analysis['detailed_analysis'], report
25
- except Exception as e:
26
- return [("error", 0)], f"Error: {str(e)}", "Error generating report"
27
 
28
- def generate_analysis(emotions):
29
- descriptions = {
30
- 'happy': 'indicates joy and positive mood',
31
- 'angry': 'suggests frustration or displeasure',
32
- 'surprise': 'shows astonishment',
33
- 'fear': 'reflects anxiety or concern',
34
- 'neutral': 'displays a balanced state'
35
- }
36
 
37
- top_emotion = max(emotions.items(), key=lambda x: x[1])
38
 
39
- return {
40
- "primary_emotion": top_emotion[0],
41
- "detailed_analysis": f"Primary emotion detected is {top_emotion[0]} ({top_emotion[1]:.1f}%), which {descriptions.get(top_emotion[0], '')}."
42
- }
43
 
44
- def generate_report(emotions, analysis):
45
- return f"""Emotion Analysis Report
46
 
47
- Primary Emotion: {analysis['primary_emotion'].upper()}
48
- Confidence: {emotions[analysis['primary_emotion']]:.1f}%
49
 
50
- All Detected Emotions:
51
- """ + '\n'.join(f"- {k}: {v:.1f}%" for k, v in sorted(
52
- emotions.items(),
53
- key=lambda x: x[1],
54
- reverse=True))
55
 
56
- # Create Gradio interface
57
- with gr.Blocks(theme=gr.themes.Soft()) as app:
58
- gr.Markdown("# Facial Emotion Analysis System")
59
 
60
- with gr.Row():
61
- with gr.Column(scale=1):
62
- input_image = gr.Image(type="numpy")
63
- submit_btn = gr.Button("Analyze", variant="primary")
64
 
65
- with gr.Column(scale=1):
66
- emotion_scores = gr.Label(label="Emotion Scores")
67
- analysis_text = gr.Textbox(label="Analysis", lines=3)
68
- report_text = gr.Textbox(label="Full Report", lines=10)
69
- download_btn = gr.Button("Download Report")
70
 
71
- submit_btn.click(
72
- fn=process_image,
73
- inputs=[input_image],
74
- outputs=[emotion_scores, analysis_text, report_text]
75
- )
76
 
77
- download_btn.click(
78
- fn=lambda x: x,
79
- inputs=[report_text],
80
- outputs=[gr.File(label="Download Report")]
81
- )
82
 
83
- app.launch()
 
1
+ import gradio as gr
2
 
3
+ gr.load("models/dima806/facial_emotions_image_detection").launch()
4
 
5
+ # import gradio as gr
6
+ # import tensorflow as tf
7
+ # from transformers import pipeline
8
 
9
+ # def process_image(image):
10
+ # try:
11
+ # # Initialize emotion classifier
12
+ # classifier = pipeline("image-classification", model="dima806/facial_emotions_image_detection")
13
 
14
+ # # Get predictions
15
+ # result = classifier(image)
16
 
17
+ # # Convert results to required format
18
+ # emotions = {item['label']: float(item['score']) * 100 for item in result}
19
 
20
+ # # Generate analysis and report
21
+ # analysis = generate_analysis(emotions)
22
+ # report = generate_report(emotions, analysis)
23
 
24
+ # return [(k, v) for k, v in emotions.items()], analysis['detailed_analysis'], report
25
+ # except Exception as e:
26
+ # return [("error", 0)], f"Error: {str(e)}", "Error generating report"
27
 
28
+ # def generate_analysis(emotions):
29
+ # descriptions = {
30
+ # 'happy': 'indicates joy and positive mood',
31
+ # 'angry': 'suggests frustration or displeasure',
32
+ # 'surprise': 'shows astonishment',
33
+ # 'fear': 'reflects anxiety or concern',
34
+ # 'neutral': 'displays a balanced state'
35
+ # }
36
 
37
+ # top_emotion = max(emotions.items(), key=lambda x: x[1])
38
 
39
+ # return {
40
+ # "primary_emotion": top_emotion[0],
41
+ # "detailed_analysis": f"Primary emotion detected is {top_emotion[0]} ({top_emotion[1]:.1f}%), which {descriptions.get(top_emotion[0], '')}."
42
+ # }
43
 
44
+ # def generate_report(emotions, analysis):
45
+ # return f"""Emotion Analysis Report
46
 
47
+ # Primary Emotion: {analysis['primary_emotion'].upper()}
48
+ # Confidence: {emotions[analysis['primary_emotion']]:.1f}%
49
 
50
+ # All Detected Emotions:
51
+ # """ + '\n'.join(f"- {k}: {v:.1f}%" for k, v in sorted(
52
+ # emotions.items(),
53
+ # key=lambda x: x[1],
54
+ # reverse=True))
55
 
56
+ # # Create Gradio interface
57
+ # with gr.Blocks(theme=gr.themes.Soft()) as app:
58
+ # gr.Markdown("# Facial Emotion Analysis System")
59
 
60
+ # with gr.Row():
61
+ # with gr.Column(scale=1):
62
+ # input_image = gr.Image(type="numpy")
63
+ # submit_btn = gr.Button("Analyze", variant="primary")
64
 
65
+ # with gr.Column(scale=1):
66
+ # emotion_scores = gr.Label(label="Emotion Scores")
67
+ # analysis_text = gr.Textbox(label="Analysis", lines=3)
68
+ # report_text = gr.Textbox(label="Full Report", lines=10)
69
+ # download_btn = gr.Button("Download Report")
70
 
71
+ # submit_btn.click(
72
+ # fn=process_image,
73
+ # inputs=[input_image],
74
+ # outputs=[emotion_scores, analysis_text, report_text]
75
+ # )
76
 
77
+ # download_btn.click(
78
+ # fn=lambda x: x,
79
+ # inputs=[report_text],
80
+ # outputs=[gr.File(label="Download Report")]
81
+ # )
82
 
83
+ # app.launch()