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|
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""ohoud alghassab (TrailTrek Gears Co) Demo
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/18P3OPYyZb-cVbGrzgPU1MkwwjhutvrmM
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
!pip install transformers gradio gtts huggingface_hub
|
| 11 |
+
|
| 12 |
+
import gradio as gr
|
| 13 |
+
from transformers import pipeline
|
| 14 |
+
from huggingface_hub import InferenceClient, login
|
| 15 |
+
from google.colab import userdata
|
| 16 |
+
import gtts
|
| 17 |
+
|
| 18 |
+
from transformers import pipeline
|
| 19 |
+
|
| 20 |
+
# Load sentiment analysis model
|
| 21 |
+
sentiment_pipeline = pipeline(
|
| 22 |
+
"sentiment-analysis",
|
| 23 |
+
model="distilbert/distilbert-base-uncased-finetuned-sst-2-english"
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# Define sentiment analysis function
|
| 27 |
+
def analyze_sentiment(text):
|
| 28 |
+
result = sentiment_pipeline(text)[0]
|
| 29 |
+
label = result["label"]
|
| 30 |
+
score = result["score"] * 100
|
| 31 |
+
return f"{label} β {score:.2f}%"
|
| 32 |
+
|
| 33 |
+
from huggingface_hub import InferenceClient, login
|
| 34 |
+
import gradio as gr
|
| 35 |
+
from google.colab import userdata
|
| 36 |
+
|
| 37 |
+
# Secure login
|
| 38 |
+
HF_TOKEN = userdata.get("HF_TOKEN")
|
| 39 |
+
login(token=HF_TOKEN)
|
| 40 |
+
|
| 41 |
+
# Load Mistral model
|
| 42 |
+
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.2")
|
| 43 |
+
|
| 44 |
+
# Response generator
|
| 45 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 46 |
+
messages = [{"role": "system", "content": system_message}]
|
| 47 |
+
for user_msg, bot_msg in history:
|
| 48 |
+
if user_msg:
|
| 49 |
+
messages.append({"role": "user", "content": user_msg})
|
| 50 |
+
if bot_msg:
|
| 51 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 52 |
+
messages.append({"role": "user", "content": message})
|
| 53 |
+
|
| 54 |
+
response = ""
|
| 55 |
+
for chunk in client.chat_completion(
|
| 56 |
+
messages,
|
| 57 |
+
max_tokens=max_tokens,
|
| 58 |
+
stream=True,
|
| 59 |
+
temperature=temperature,
|
| 60 |
+
top_p=top_p,
|
| 61 |
+
):
|
| 62 |
+
token = chunk.choices[0].delta.content or ""
|
| 63 |
+
response += token
|
| 64 |
+
yield response
|
| 65 |
+
|
| 66 |
+
from transformers import pipeline
|
| 67 |
+
|
| 68 |
+
# Load summarization pipeline
|
| 69 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 70 |
+
|
| 71 |
+
# Define summarization function
|
| 72 |
+
def summarize_text(text):
|
| 73 |
+
summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
|
| 74 |
+
return summary[0]["summary_text"]
|
| 75 |
+
|
| 76 |
+
import gtts
|
| 77 |
+
|
| 78 |
+
# Define text-to-speech function
|
| 79 |
+
def text_to_speech(text):
|
| 80 |
+
tts = gtts.gTTS(text)
|
| 81 |
+
tts.save("output.mp3")
|
| 82 |
+
return "output.mp3"
|
| 83 |
+
|
| 84 |
+
"""*π* Gradio Multi-Tab Interface
|
| 85 |
+
(final step to connect all functions above into UI)
|
| 86 |
+
"""
|
| 87 |
+
|
| 88 |
+
import gradio as gr
|
| 89 |
+
|
| 90 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple")) as demo:
|
| 91 |
+
gr.Image(
|
| 92 |
+
value="https://www.leaders-mena.com/leaders/uploads/2024/09/Tuwaiq-Meta-780x470.jpg",
|
| 93 |
+
show_label=False,
|
| 94 |
+
show_download_button=False,
|
| 95 |
+
height=120
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
gr.Markdown(
|
| 99 |
+
"<h1 style='text-align: center; color: orange;'>π TrailTrek Gears - All-in-One AI App</h1>",
|
| 100 |
+
elem_id="main-title"
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# π Sentiment Analysis Tab
|
| 104 |
+
with gr.Tab("π Sentiment Analysis"):
|
| 105 |
+
sentiment_input = gr.Textbox(label="Enter your text")
|
| 106 |
+
sentiment_output = gr.Textbox(label="Sentiment Result")
|
| 107 |
+
sentiment_btn = gr.Button("Analyze Sentiment")
|
| 108 |
+
sentiment_btn.click(analyze_sentiment, inputs=sentiment_input, outputs=sentiment_output)
|
| 109 |
+
|
| 110 |
+
# π¬ Chatbot Tab
|
| 111 |
+
with gr.Tab("π¬ Chatbot"):
|
| 112 |
+
gr.Markdown("### Simple Chat with Mistral-7B")
|
| 113 |
+
chatbot_output = gr.Textbox(label="Chat History", lines=10, interactive=False, show_copy_button=True)
|
| 114 |
+
chatbot_input = gr.Textbox(label="Your Message", placeholder="Type something...")
|
| 115 |
+
chatbot_history = gr.State([])
|
| 116 |
+
|
| 117 |
+
def custom_chat_simple(user_input, history):
|
| 118 |
+
if history is None:
|
| 119 |
+
history = []
|
| 120 |
+
system_message = "You are a helpful and polite assistant."
|
| 121 |
+
gen = respond(user_input, history, system_message, 256, 0.5, 0.95)
|
| 122 |
+
final_response = ""
|
| 123 |
+
for chunk in gen:
|
| 124 |
+
final_response = chunk
|
| 125 |
+
history.append((user_input, final_response))
|
| 126 |
+
chat_display = ""
|
| 127 |
+
for user, bot in history:
|
| 128 |
+
chat_display += f"π§: {user}\nπ€: {bot}\n\n"
|
| 129 |
+
return chat_display, history
|
| 130 |
+
|
| 131 |
+
send_button = gr.Button("Send")
|
| 132 |
+
send_button.click(fn=custom_chat_simple, inputs=[chatbot_input, chatbot_history], outputs=[chatbot_output, chatbot_history])
|
| 133 |
+
|
| 134 |
+
# βοΈ Summarization Tab
|
| 135 |
+
with gr.Tab("βοΈ Summarization"):
|
| 136 |
+
input_text = gr.Textbox(lines=8, label="Enter long text")
|
| 137 |
+
output_summary = gr.Textbox(label="Summary")
|
| 138 |
+
summarize_btn = gr.Button("Summarize")
|
| 139 |
+
summarize_btn.click(summarize_text, inputs=input_text, outputs=output_summary)
|
| 140 |
+
|
| 141 |
+
# π Text-to-Speech Tab
|
| 142 |
+
with gr.Tab("π Text-to-Speech"):
|
| 143 |
+
tts_input = gr.Textbox(label="Enter text to convert to audio")
|
| 144 |
+
tts_output = gr.Audio(label="Generated Speech")
|
| 145 |
+
tts_btn = gr.Button("Generate Audio")
|
| 146 |
+
tts_btn.click(text_to_speech, inputs=tts_input, outputs=tts_output)
|
| 147 |
+
|
| 148 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
gradio
|
| 3 |
+
gtts
|
| 4 |
+
huggingface_hub
|
| 5 |
+
Torch
|
| 6 |
+
google.colab
|