bobo-dada commited on
Commit
6fd4538
·
verified ·
1 Parent(s): d247243

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -0
app.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
3
+ import requests
4
+ from bs4 import BeautifulSoup
5
+
6
+ sentiment_pipeline = pipeline("sentiment-analysis") # 1-Sentiment Analysis Pipeline
7
+
8
+ def get_sentiment(text):
9
+ result = sentiment_pipeline(text)[0]
10
+ sentiment = result['label']
11
+ confidence = result['score']
12
+ return sentiment, confidence
13
+ ########################################################
14
+
15
+ chatbot_tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium") # 2-Chatbot Pipeline
16
+
17
+ chatbot_model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
18
+
19
+ def generate_response(message, history):
20
+ # Encode the input message
21
+ input_ids = chatbot_tokenizer.encode(message + chatbot_tokenizer.eos_token, return_tensors="pt")
22
+
23
+ # Generate response
24
+ response_ids = chatbot_model.generate(
25
+ input_ids,
26
+ max_length=1000,
27
+ pad_token_id=chatbot_tokenizer.eos_token_id,
28
+ no_repeat_ngram_size=3,
29
+ do_sample=True,
30
+ top_k=100,
31
+ top_p=0.7,
32
+ temperature=0.8
33
+ )
34
+
35
+ # Decode the response
36
+ response = chatbot_tokenizer.decode(response_ids[0], skip_special_tokens=True)
37
+ return response
38
+
39
+ ########################################################
40
+
41
+ summary_pipeline = pipeline("summarization", model="Falconsai/text_summarization") # 3-Summarization Pipeline
42
+
43
+ def summarize_url(url):
44
+ try:
45
+ data = requests.get(url)
46
+ soup = BeautifulSoup(data.content, "html.parser")
47
+ article = soup.find("article")
48
+ if article:
49
+ text = article.text.strip()
50
+ summary = summary_pipeline(text, max_length=512, truncation=True)[0]['summary_text']
51
+ return summary
52
+ else:
53
+ return "Could not find an article on the provided URL."
54
+ except Exception as e:
55
+ return f"Error: {str(e)}"
56
+ ########################################################
57
+
58
+ transcription_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-small") # 4-Speech Recognition Pipeline
59
+
60
+ def transcribe_audio(audio_file):
61
+ try:
62
+ transcription = transcription_pipeline(audio_file)["text"]
63
+ return transcription
64
+ except Exception as e:
65
+ return f"Error during transcription: {str(e)}"
66
+ ########################################################
67
+
68
+
69
+ with gr.Blocks() as interface:
70
+ gr.Markdown("# Multi-Model Model on Gardio") # Our Gradio Interface
71
+
72
+
73
+ with gr.Tabs():
74
+ with gr.Tab("Sentiment Analysis"):
75
+ gr.Markdown("Enter a sentence to analyze its sentiment and confidence score.")
76
+ text_input = gr.Textbox(label="Enter text")
77
+ sentiment_output = gr.Textbox(label='Sentiment')
78
+ confidence_output = gr.Textbox(label='Confidence Score')
79
+ analyze_button = gr.Button("Analyze")
80
+ analyze_button.click(get_sentiment, inputs=text_input, outputs=[sentiment_output, confidence_output])
81
+
82
+ with gr.Tab("Summarization"):
83
+ gr.Markdown("Enter a news article URL to get a summary.")
84
+ url_input = gr.Textbox(label="Article URL")
85
+ summary_output = gr.Textbox(label="Summary", lines=5)
86
+ summarize_button = gr.Button("Summarize")
87
+ summarize_button.click(summarize_url, inputs=url_input, outputs=summary_output)
88
+
89
+ with gr.Tab("Speech Recognition"):
90
+ gr.Markdown("Upload an audio file for transcription.")
91
+ audio_input = gr.Audio(label="Upload Audio", type="filepath")
92
+ transcription_output = gr.Textbox(label="Transcription", lines=3)
93
+ transcribe_button = gr.Button("Transcribe")
94
+ transcribe_button.click(transcribe_audio, inputs=audio_input, outputs=transcription_output)
95
+
96
+ with gr.Tab("Chatbot"):
97
+ gr.Markdown("Have a conversation with the AI chatbot.")
98
+ chatbot = gr.Chatbot(
99
+ label="Chat History",
100
+ height=400
101
+ )
102
+ msg = gr.Textbox(
103
+ label="Type your message",
104
+ placeholder="Type your message here...",
105
+ show_label=False
106
+ )
107
+ clear = gr.Button("Clear")
108
+
109
+ def user(user_message, history):
110
+ return "", history + [[user_message, None]]
111
+
112
+ def bot(history):
113
+ user_message = history[-1][0]
114
+ bot_message = generate_response(user_message, history)
115
+ history[-1][1] = bot_message
116
+ return history
117
+
118
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
119
+ bot, chatbot, chatbot
120
+ )
121
+ clear.click(lambda: None, None, chatbot, queue=False)
122
+
123
+ if __name__ =="__main__": ## running my app on hugging face
124
+ interface.launch()