Spaces:
Build error
Build error
File size: 4,318 Bytes
22c93d7 3dc1c5a 22c93d7 bcc117b 7eff33b 3dc1c5a 22c93d7 bcc117b 22c93d7 bcc117b 3dc1c5a bcc117b 22c93d7 3dc1c5a bcc117b 22c93d7 3dc1c5a 22c93d7 3dc1c5a bcc117b 3dc1c5a 6fd93dc 3dc1c5a bcc117b 3dc1c5a bcc117b 3dc1c5a bcc117b 3dc1c5a 22c93d7 3dc1c5a | 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 | # Import required libraries
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
import torch
import gradio as gr
# Initialize AI pipelines
sentiment = pipeline("sentiment-analysis", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
summarize = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
# Initialize ASR with a specific model
asr = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h")
# Load chatbot model and tokenizer
chatbot_tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
chatbot_model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
# generating a response from the model
def generate_response(message, history):
# Encode the input message
input_ids = chatbot_tokenizer.encode(message + chatbot_tokenizer.eos_token, return_tensors="pt")
# Generate response
response_ids = chatbot_model.generate(
input_ids,
max_length=1000,
pad_token_id=chatbot_tokenizer.eos_token_id,
no_repeat_ngram_size=3,
do_sample=True,
top_k=100,
top_p=0.7,
temperature=0.8
)
# Decode the response
response = chatbot_tokenizer.decode(response_ids[0], skip_special_tokens=True)
return response
# Functions for each task
def get_sentiment(text):
sent = sentiment(text)[0]['label']
score = sentiment(text)[0]['score']
return sent, score
def transcribe_audio(speech):
text = asr(speech)["text"]
return text
def summ(text):
text = summarize(text)
return text
# Chatbot function
def chat_response(message, history):
# Encode the input text
inputs = tokenizer(message, return_tensors="pt")
# Generate response
outputs = model.generate(
inputs.input_ids,
max_length=100,
num_return_sequences=1,
temperature=0.7,
pad_token_id=tokenizer.eos_token_id
)
# Decode and return response
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
return response
# Create Gradio interface
interface = gr.Blocks()
# Building the block interface with tabs
with interface:
gr.Markdown("# TrailTrek Co")
with gr.Tabs():
with gr.TabItem("Sentiment Analysis"):
with gr.Row():
text_input = gr.Textbox(label="Enter the review")
with gr.Row():
text_output = [gr.Textbox(label = "Sentiment :"), gr.Textbox(label = "Score :")]
analyze_butt = gr.Button("Analyze")
with gr.TabItem("Summarize"):
with gr.Row():
summ_input = gr.Textbox(label="Text to summarize")
summ_output = gr.Textbox(label="Result")
summ_button = gr.Button("Summarize")
with gr.Tab("Chatbot"):
gr.Markdown("Say hello :)")
chatbot = gr.Chatbot(
label="Chat History",
height=400
)
msg = gr.Textbox(
label="Type your message",
placeholder="Type your message here...",
show_label=False
)
clear = gr.Button("Clear")
def user(user_message, history):
return "", history + [[user_message, None]]
def bot(history):
user_message = history[-1][0]
bot_message = generate_response(user_message, history)
history[-1][1] = bot_message
return history
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot, chatbot, chatbot
)
clear.click(lambda: None, None, chatbot, queue=False)
with gr.Tab("Speech Recognition"):
gr.Markdown("Upload an audio file for transcription.")
audio_input = gr.Audio(label="Upload Audio", type="filepath")
transcription_output = gr.Textbox(label="Transcription", lines=3)
transcribe_butt = gr.Button("Transcribe")
analyze_butt.click(get_sentiment, inputs=text_input, outputs=text_output)
summ_button.click(summarize, inputs=summ_input, outputs=summ_output)
transcribe_butt.click(transcribe_audio, inputs=audio_input, outputs=transcription_output)
# Launch the interface
interface.launch() |