# -*- coding: utf-8 -*- """ohoud alghassab (TrailTrek Gears Co) Demo Automatically generated by Colab. Original file is located at https://colab.research.google.com/drive/18P3OPYyZb-cVbGrzgPU1MkwwjhutvrmM """ !pip install transformers gradio gtts huggingface_hub import gradio as gr from transformers import pipeline from huggingface_hub import InferenceClient, login from google.colab import userdata import gtts from transformers import pipeline # Load sentiment analysis model sentiment_pipeline = pipeline( "sentiment-analysis", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english" ) # Define sentiment analysis function def analyze_sentiment(text): result = sentiment_pipeline(text)[0] label = result["label"] score = result["score"] * 100 return f"{label} — {score:.2f}%" from huggingface_hub import InferenceClient, login import gradio as gr from google.colab import userdata # Secure login HF_TOKEN = userdata.get("HF_TOKEN") login(token=HF_TOKEN) # Load Mistral model client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.2") # Response generator def respond(message, history, system_message, max_tokens, temperature, top_p): messages = [{"role": "system", "content": system_message}] for user_msg, bot_msg in history: if user_msg: messages.append({"role": "user", "content": user_msg}) if bot_msg: messages.append({"role": "assistant", "content": bot_msg}) messages.append({"role": "user", "content": message}) response = "" for chunk in client.chat_completion( messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p, ): token = chunk.choices[0].delta.content or "" response += token yield response from transformers import pipeline # Load summarization pipeline summarizer = pipeline("summarization", model="facebook/bart-large-cnn") # Define summarization function def summarize_text(text): summary = summarizer(text, max_length=130, min_length=30, do_sample=False) return summary[0]["summary_text"] import gtts # Define text-to-speech function def text_to_speech(text): tts = gtts.gTTS(text) tts.save("output.mp3") return "output.mp3" """*šŸš€* Gradio Multi-Tab Interface (final step to connect all functions above into UI) """ import gradio as gr with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple")) as demo: gr.Image( value="https://www.leaders-mena.com/leaders/uploads/2024/09/Tuwaiq-Meta-780x470.jpg", show_label=False, show_download_button=False, height=120 ) gr.Markdown( "

🌟 TrailTrek Gears - All-in-One AI App

", elem_id="main-title" ) # šŸ“Š Sentiment Analysis Tab with gr.Tab("šŸ“Š Sentiment Analysis"): sentiment_input = gr.Textbox(label="Enter your text") sentiment_output = gr.Textbox(label="Sentiment Result") sentiment_btn = gr.Button("Analyze Sentiment") sentiment_btn.click(analyze_sentiment, inputs=sentiment_input, outputs=sentiment_output) # šŸ’¬ Chatbot Tab with gr.Tab("šŸ’¬ Chatbot"): gr.Markdown("### Simple Chat with Mistral-7B") chatbot_output = gr.Textbox(label="Chat History", lines=10, interactive=False, show_copy_button=True) chatbot_input = gr.Textbox(label="Your Message", placeholder="Type something...") chatbot_history = gr.State([]) def custom_chat_simple(user_input, history): if history is None: history = [] system_message = "You are a helpful and polite assistant." gen = respond(user_input, history, system_message, 256, 0.5, 0.95) final_response = "" for chunk in gen: final_response = chunk history.append((user_input, final_response)) chat_display = "" for user, bot in history: chat_display += f"šŸ§‘: {user}\nšŸ¤–: {bot}\n\n" return chat_display, history send_button = gr.Button("Send") send_button.click(fn=custom_chat_simple, inputs=[chatbot_input, chatbot_history], outputs=[chatbot_output, chatbot_history]) # āœ‚ļø Summarization Tab with gr.Tab("āœ‚ļø Summarization"): input_text = gr.Textbox(lines=8, label="Enter long text") output_summary = gr.Textbox(label="Summary") summarize_btn = gr.Button("Summarize") summarize_btn.click(summarize_text, inputs=input_text, outputs=output_summary) # šŸ”ˆ Text-to-Speech Tab with gr.Tab("šŸ”ˆ Text-to-Speech"): tts_input = gr.Textbox(label="Enter text to convert to audio") tts_output = gr.Audio(label="Generated Speech") tts_btn = gr.Button("Generate Audio") tts_btn.click(text_to_speech, inputs=tts_input, outputs=tts_output) demo.launch()