Pixel101 commited on
Commit
078d614
ยท
verified ยท
1 Parent(s): 575d723

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -81
app.py CHANGED
@@ -1,22 +1,22 @@
1
- # prompt: conbine all the models above in one gradio interface , and well customized
 
2
 
3
  import gradio as gr
4
  from transformers import pipeline
5
- import csv
6
- import heapq as hq
7
- from google.colab import files
8
-
9
 
 
10
  # Sentiment Analysis
11
  classifier_sentiment = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
 
12
  def analyze_sentiment(text):
13
  result = classifier_sentiment(text)[0]
14
  label = result['label']
15
  score = result['score']
16
- return f"Label: {label},"
17
 
18
  # Translation
19
  translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")
 
20
  def translate_text(text):
21
  result = translator(text)[0]
22
  translated_text = result["translation_text"]
@@ -24,111 +24,81 @@ def translate_text(text):
24
 
25
  # Image Classification
26
  classifier_image = pipeline("image-classification", model="google/mobilenet_v2_1.0_224")
 
27
  def classify_image(image):
28
- results = classifier_image(image)
29
- output = ""
30
- for result in results:
31
- output += f"{result['label']}: {result['score']:.2f}\n"
32
- return output
33
 
34
  # Speech to Text
35
  speech_to_text = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h")
 
36
  def transcribe_audio(audio):
37
  text = speech_to_text(audio)["text"]
38
  return text
39
 
40
  # Text Summarization
41
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
 
42
  def summarize_text(text):
43
- summary = summarizer(text, max_length=130, min_length=30, do_sample=False)[0]["summary_text"]
44
- return summary
45
-
46
- # Woodall Number
47
- def is_woodall(x):
48
- if (x % 2 == 0):
49
- return False
50
- if (x == 1):
51
- return True
52
- x = x + 1
53
- p = 0
54
- while (x % 2 == 0):
55
- x = x/2
56
- p = p + 1
57
- if (p == x):
58
- return True
59
- return False
60
-
61
-
62
- # Largest Numbers from a List (Heap Queue)
63
- def heap_queue_largest(nums,n):
64
- try:
65
- largest_nums = hq.nlargest(n, nums)
66
- return largest_nums
67
- except Exception as e:
68
- return f"Error: {e}"
69
-
70
-
71
- # CSV Reader
72
- def csv_reader(filepath):
73
- try:
74
- with open(filepath, 'r') as f:
75
- reader = csv.reader(f)
76
- data = [row for row in reader]
77
- return data
78
- except Exception as e:
79
- return f"Error: {e}"
80
-
81
-
82
- with gr.Blocks() as demo:
83
- gr.Markdown("<h1>Multi-functional AI Demo</h1>")
84
-
85
- with gr.Tab("Sentiment Analysis"):
86
  text_input = gr.Textbox(placeholder="Enter text here...")
87
  text_output = gr.Textbox()
88
  sentiment_button = gr.Button("Analyze")
89
  sentiment_button.click(analyze_sentiment, inputs=text_input, outputs=text_output)
90
 
91
- with gr.Tab("Translation"):
92
- text_input_trans = gr.Textbox(placeholder="Enter English text here...")
93
- text_output_trans = gr.Textbox()
94
- trans_button = gr.Button("Translate")
95
- trans_button.click(translate_text, inputs=text_input_trans, outputs=text_output_trans)
96
 
97
- with gr.Tab("Image Classification"):
98
  image_input = gr.Image(type="pil")
99
  image_output = gr.Textbox()
100
  image_button = gr.Button("Classify")
101
  image_button.click(classify_image, inputs=image_input, outputs=image_output)
102
 
103
- with gr.Tab("Speech to Text"):
104
  audio_input = gr.Audio(sources=["microphone"], type="filepath")
105
  audio_output = gr.Textbox()
106
  audio_button = gr.Button("Transcribe")
107
  audio_button.click(transcribe_audio, inputs=audio_input, outputs=audio_output)
108
 
109
- with gr.Tab("Text Summarization"):
110
  text_input_summ = gr.Textbox(placeholder="Enter text here...")
111
  text_output_summ = gr.Textbox()
112
  summ_button = gr.Button("Summarize")
113
  summ_button.click(summarize_text, inputs=text_input_summ, outputs=text_output_summ)
114
 
115
- with gr.Tab("Woodall Number"):
116
- woodall_input = gr.Number(label="Enter a number:")
117
- woodall_output = gr.Textbox()
118
- woodall_button = gr.Button("Check")
119
- woodall_button.click(is_woodall, inputs=woodall_input, outputs=woodall_output)
120
-
121
- with gr.Tab("Largest Numbers (Heap Queue)"):
122
- nums_input = gr.Textbox(label="Enter numbers separated by spaces:")
123
- n_input = gr.Number(label="Number of largest elements:")
124
- nums_output = gr.Textbox()
125
- nums_button = gr.Button("Find Largest")
126
- nums_button.click(heap_queue_largest, inputs=[nums_input, n_input], outputs=nums_output)
127
-
128
- with gr.Tab("CSV Reader"):
129
- csv_input = gr.File(label="Upload CSV File")
130
- csv_output = gr.Textbox()
131
- csv_button = gr.Button("Read CSV")
132
- csv_button.click(csv_reader, inputs=csv_input, outputs=csv_output)
133
-
134
  demo.launch()
 
1
+ # Install necessary libraries
2
+ !pip install transformers gradio librosa
3
 
4
  import gradio as gr
5
  from transformers import pipeline
 
 
 
 
6
 
7
+ # Load models
8
  # Sentiment Analysis
9
  classifier_sentiment = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
10
+
11
  def analyze_sentiment(text):
12
  result = classifier_sentiment(text)[0]
13
  label = result['label']
14
  score = result['score']
15
+ return f"Label: {label}, Score: {score:.2f}"
16
 
17
  # Translation
18
  translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")
19
+
20
  def translate_text(text):
21
  result = translator(text)[0]
22
  translated_text = result["translation_text"]
 
24
 
25
  # Image Classification
26
  classifier_image = pipeline("image-classification", model="google/mobilenet_v2_1.0_224")
27
+
28
  def classify_image(image):
29
+ results = classifier_image(image)
30
+ output = ""
31
+ for result in results:
32
+ output += f"{result['label']}: {result['score']:.2f}\n"
33
+ return output
34
 
35
  # Speech to Text
36
  speech_to_text = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h")
37
+
38
  def transcribe_audio(audio):
39
  text = speech_to_text(audio)["text"]
40
  return text
41
 
42
  # Text Summarization
43
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
44
+
45
  def summarize_text(text):
46
+ summary = summarizer(text, max_length=130, min_length=30, do_sample=False)[0]["summary_text"]
47
+ return summary
48
+
49
+ # Define custom CSS styles
50
+ css = """
51
+ <style>
52
+ body {
53
+ background-color: #e9ecef;
54
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
55
+ }
56
+ .gradio-container {
57
+ border-radius: 15px;
58
+ box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
59
+ padding: 20px;
60
+ background-color: #74748a;
61
+ max-width: 800px;
62
+ margin: auto;
63
+ }
64
+ h1 {
65
+ color: black;
66
+ }
67
+
68
+ </style>
69
+ """
70
+
71
+ with gr.Blocks(css=css) as demo:
72
+ gr.Markdown("<h1 style='text-align: center;'>Multi-functional AI Demo</h1>")
73
+
74
+ with gr.Tab("Sentiment Analysis๐Ÿ˜ฃ"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  text_input = gr.Textbox(placeholder="Enter text here...")
76
  text_output = gr.Textbox()
77
  sentiment_button = gr.Button("Analyze")
78
  sentiment_button.click(analyze_sentiment, inputs=text_input, outputs=text_output)
79
 
80
+ with gr.Tab("Translation๐Ÿ“š"):
81
+ text_input_trans = gr.Textbox(placeholder="Enter English text here...")
82
+ text_output_trans = gr.Textbox()
83
+ trans_button = gr.Button("Translate")
84
+ trans_button.click(translate_text, inputs=text_input_trans, outputs=text_output_trans)
85
 
86
+ with gr.Tab("Image Classification๐Ÿ”ฎ"):
87
  image_input = gr.Image(type="pil")
88
  image_output = gr.Textbox()
89
  image_button = gr.Button("Classify")
90
  image_button.click(classify_image, inputs=image_input, outputs=image_output)
91
 
92
+ with gr.Tab("Speech to Text๐Ÿ”Š"):
93
  audio_input = gr.Audio(sources=["microphone"], type="filepath")
94
  audio_output = gr.Textbox()
95
  audio_button = gr.Button("Transcribe")
96
  audio_button.click(transcribe_audio, inputs=audio_input, outputs=audio_output)
97
 
98
+ with gr.Tab("Text Summarization๐Ÿ“‘"):
99
  text_input_summ = gr.Textbox(placeholder="Enter text here...")
100
  text_output_summ = gr.Textbox()
101
  summ_button = gr.Button("Summarize")
102
  summ_button.click(summarize_text, inputs=text_input_summ, outputs=text_output_summ)
103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  demo.launch()