Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py.py +116 -0
- requirements.txt +4 -0
app.py.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""[Student]_Module_2_Session_3[1].ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1XTqd7vFIlHX6bgIBkZf2UUTw9ekrOO0T
|
| 8 |
+
|
| 9 |
+
Installations
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
"""#Let's build a demo for a sentiment analysis task !
|
| 14 |
+
|
| 15 |
+
Import the necessary modules :
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
from transformers import pipeline
|
| 19 |
+
import gradio as gr
|
| 20 |
+
|
| 21 |
+
"""Import the pipeline :"""
|
| 22 |
+
|
| 23 |
+
sentiment = pipeline("sentiment-analysis")
|
| 24 |
+
|
| 25 |
+
"""Test the pipeline on these reviews (you can also test on your own reviews) :"""
|
| 26 |
+
|
| 27 |
+
#"I really enjoyed my stay !"
|
| 28 |
+
#"Worst rental I ever got"
|
| 29 |
+
|
| 30 |
+
sentiment(["I really enjoyed my stay !","Worst rental I ever got"])
|
| 31 |
+
|
| 32 |
+
"""What is the format of the output ? How can you get only the sentiment or the confidence score ?"""
|
| 33 |
+
|
| 34 |
+
sentiment(["I really enjoyed my stay !","Worst rental I ever got"])[0]["label"]
|
| 35 |
+
|
| 36 |
+
sentiment(["I really enjoyed my stay !","Worst rental I ever got"])[0]["score"]
|
| 37 |
+
|
| 38 |
+
"""Create a function that takes a text in input, and returns a sentiment, and a confidence score as 2 different variables"""
|
| 39 |
+
|
| 40 |
+
def get_sentiment(text):
|
| 41 |
+
lbl = sentiment(text)[0]["label"]
|
| 42 |
+
scr = sentiment(text)[0]["score"]
|
| 43 |
+
return lbl,scr
|
| 44 |
+
|
| 45 |
+
summarizer = pipeline("summarization")
|
| 46 |
+
|
| 47 |
+
def get_sum(text):
|
| 48 |
+
return summarizer(text)[0]["summary_text"]
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
from gtts import gTTS
|
| 53 |
+
|
| 54 |
+
def convert(text):
|
| 55 |
+
tts = gTTS(text)
|
| 56 |
+
path = "audio.mp3"
|
| 57 |
+
tts.save(path)
|
| 58 |
+
return path
|
| 59 |
+
|
| 60 |
+
#to get access to the model on hugging face
|
| 61 |
+
from huggingface_hub import InferenceClient
|
| 62 |
+
#You are creating a connection to a model hosted on Hugging Face using their InferenceClient.
|
| 63 |
+
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.2")
|
| 64 |
+
|
| 65 |
+
# Parameters:
|
| 66 |
+
# - message: the new message the user just sent (like "What's the weather?")
|
| 67 |
+
# - history: a list of all the previous messages in the chat
|
| 68 |
+
def respond(message, history):
|
| 69 |
+
|
| 70 |
+
#Why use this structure?
|
| 71 |
+
#Because the API expects the messages in an ordered format, where each message has a role (system, user, assistant) and content.
|
| 72 |
+
#We need to keep the conversation order, so a list is used (order matters).
|
| 73 |
+
#Each message has structured data (role and content), so a dictionary is used.
|
| 74 |
+
#The list contains elements, and each element here is a dictionary.
|
| 75 |
+
# This tells the model how to behave (like giving it a "job description").
|
| 76 |
+
messages = [{"role": "system", "content": "You are a helpful formal assistant."}]
|
| 77 |
+
|
| 78 |
+
# This loop recreates the entire chat history in a format the model understands.
|
| 79 |
+
# It adds each user question and the assistant's response in order.
|
| 80 |
+
for user, bot in history:
|
| 81 |
+
messages.append({"role": "user", "content": user})
|
| 82 |
+
messages.append({"role": "assistant", "content": bot})
|
| 83 |
+
|
| 84 |
+
# Add the new message to the end of the conversation
|
| 85 |
+
messages.append({"role": "user", "content": message})
|
| 86 |
+
|
| 87 |
+
# Send the conversation to the Mistral model via Hugging Face
|
| 88 |
+
# max_tokens=300 limits how long the reply can be
|
| 89 |
+
response = client.chat_completion(messages, max_tokens=300)
|
| 90 |
+
|
| 91 |
+
# Extract the first reply from the model's response
|
| 92 |
+
bot_reply = response.choices[0].message.content
|
| 93 |
+
|
| 94 |
+
return bot_reply
|
| 95 |
+
|
| 96 |
+
convo_tab = gr.ChatInterface(fn = respond,title = "Chatbot", description = "feel free to ask anything!")
|
| 97 |
+
|
| 98 |
+
conv_tab = gr.Interface(fn = convert, inputs = gr.Textbox(label= "Enter your text here: "), outputs = gr.Audio(label = "Here's the audio: "), title= "Text-to-Speech")
|
| 99 |
+
|
| 100 |
+
"""Build an interface for the app using Gradio.
|
| 101 |
+
The customer wants this result :
|
| 102 |
+
|
| 103 |
+

|
| 104 |
+
"""
|
| 105 |
+
|
| 106 |
+
sen_tab = gr.Interface(fn = get_sentiment, inputs = gr.Textbox(label="Enter your review: ",placeholder= "(eg..I love this product)"),outputs = [gr.Textbox(label="Sentiment:"),gr.Number(label="Score:")], title = "Sentiment Analysis" )
|
| 107 |
+
|
| 108 |
+
sum_tab = gr.Interface(fn = get_sum, inputs = gr.Textbox(label= "Enter your text to summarize: "), outputs = gr.Textbox(label="Summarization: "), title = "Summarization")
|
| 109 |
+
|
| 110 |
+
demo = gr.TabbedInterface(
|
| 111 |
+
[sen_tab, sum_tab, conv_tab, convo_tab],
|
| 112 |
+
["Sentiment-Analysis","Summarization", "Text-To-Speech","Chatbot"],
|
| 113 |
+
theme = "soft"
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
gtts
|