Spaces:
Sleeping
Sleeping
File size: 4,405 Bytes
1b0d0fd 5eb6bdd 1b0d0fd 5eb6bdd 1b0d0fd 5eb6bdd 1b0d0fd 5eb6bdd 1b0d0fd c94a321 1b0d0fd 5eb6bdd 1b0d0fd 5eb6bdd 1b0d0fd 5eb6bdd 1b0d0fd 5eb6bdd 1b0d0fd 5eb6bdd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
import gradio as gr
from transformers import pipeline
import torch
from huggingface_hub import InferenceClient
# Initialize pipeline
sentiment_analyzer = pipeline("text-classification", model="tabularisai/multilingual-sentiment-analysis")
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
chatbot = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
speech_model = pipeline("automatic-speech-recognition", model="openai/whisper-small")
# Sentiment analysis functoin that takes text and returns sentiment and confidence
def analyze_sentiment(text):
result = sentiment_analyzer(text)[0]
return f"Sentiment: {result['label']}", f"Confidence: {result['score']:.2f}"
# Function where users can engage in a conversational interface.
def chat_response(
message,
max_tokens = 500,
temperature = 0.8,
top_p = 0.8,
):
messages = [{"role": "system", "content": 'You are an assistant for a company called TrailTrek Gears Co specialized in Hiking'}]
messages.append({"role": "user", "content": message})
response = ""
for message in chatbot.chat_completion(
messages,
max_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
):
token = message.choices[0].delta.content
response += token
yield response
# A summarization function
def summarize_text(text):
# Ensure text is within model's max length
max_length = 1024
if len(text.split()) > max_length:
text = " ".join(text.split()[:max_length])
summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
return summary[0]['summary_text']
# A function that takes text and outputs a speech of the text
def speech_to_text(audio):
if audio is None:
return "Please upload an audio file or record audio to transcribe."
# Transcribe the audio
result = speech_model(audio)
return result["text"]
with gr.Blocks(title="TrailTrek Gears AI Suite") as demo:
gr.Markdown("# TrailTrek Gears AI Suite")
with gr.Tab("Sentiment Analysis"):
gr.Markdown("### Analyze the sentiment of your text")
with gr.Row():
sentiment_input = gr.Textbox(label="Enter text to analyze", lines=4)
with gr.Column():
sentiment_label = gr.Textbox(label="Sentiment")
confidence_score = gr.Textbox(label="Confidence Score")
sentiment_button = gr.Button("Analyze Sentiment")
sentiment_button.click(
analyze_sentiment,
inputs=sentiment_input,
outputs=[sentiment_label, confidence_score]
)
with gr.Tab("Chatbot"):
gr.Markdown("# Stateless Chatbot using Zephyr-7b-beta")
with gr.Row():
chat_input = gr.Textbox(
placeholder="Type your message here...",
label="Your Message"
)
chat_output = gr.Textbox(
label="Assistant Response",
interactive=False
)
chat_input.submit(chat_response, inputs=chat_input, outputs=chat_output)
with gr.Tab("Summarization"):
gr.Markdown("### Get a concise summary of your text")
with gr.Row():
summary_input = gr.Textbox(label="Enter text to summarize", lines=8)
summary_output = gr.Textbox(label="Summary", lines=4)
summary_button = gr.Button("Generate Summary")
summary_button.click(
summarize_text,
inputs=summary_input,
outputs=summary_output
)
with gr.Tab("Speech-to-Text"):
gr.Markdown("### Convert speech to text using Whisper")
with gr.Row():
with gr.Column():
audio = gr.Audio(
"microphone",
type="filepath",
label="Record or upload audio"
)
transcribe_button = gr.Button("Transcribe Audio")
text_output = gr.Textbox(
label="Transcribed Text",
lines=4,
placeholder="Transcribed text will appear here..."
)
transcribe_button.click(
speech_to_text,
inputs=audio,
outputs=text_output
)
# Launch the app
if __name__ == "__main__":
demo.launch() |