Chad-1 / app.py
IBKcode's picture
Update app.py
3036a2a verified
# === IMPORTS FOR CHAD-1 ===
from transformers import pipeline
import torch
import time
import gradio as gr
# --- MODEL LOADING ---
text_model = pipeline(
"text2text-generation",
model="google/flan-t5-base",
device=-1)
emotion_model = pipeline(
"text-classification",
model="SamLowe/roberta-base-go_emotions",
device=-1,
return_all_scores=True
)
# --- FUNCTIONS ---
def summarize(text):
summary = text_model(
f"summarize: {text}",
max_new_tokens=250,
min_length=70,
do_sample=True,
top_k=50,
top_p=0.95,
temperature=0.9,
num_beams=3
)
return summary[0]['generated_text']
def answer_question(context, question):
prompt = f"Answer the question based on the context:\nContext: {context}\nQuestion: {question}"
answer = text_model(prompt, max_new_tokens=100)
return answer[0]['generated_text']
def classify_emotion(sentence):
emotions = emotion_model(sentence)
top_emotion = max(emotions[0], key=lambda x: x['score'])
return top_emotion['label']
# --- GRADIO WRAPPERS ---
def gr_summarize(text):
return summarize(text)
def gr_qa(context, question):
return answer_question(context, question)
def gr_emotion(sentence):
return classify_emotion(sentence)
# --- CHAD-1 GRADIO UI ---
with gr.Blocks(css="""
body {background-color: black; color: red;}
.gr-button {background-color: red; color: black;}
.gr-box {background-color: #111111; color: red;}
.gr-textbox textarea {background-color: #111111; color: red;}
.gr-tabs {background-color: #111111; color: red;}
""") as chad_app:
with gr.Tab("Homepage"):
gr.Markdown("""
## Welcome to Chad-1: A Text Intelligence Assistant
Developed by Olaleye Faithfulness Ibukun
Use the tabs above to Summarize text, Ask a question, or Classify emotion.
""")
with gr.Tab("Summarize"):
input_text = gr.Textbox(label="Enter text to summarize", placeholder="Enter the text you want summarized", lines=6)
output_summary = gr.Textbox(label="Summary", lines=6)
btn_summarize = gr.Button("Summarize")
btn_summarize.click(fn=gr_summarize, inputs=input_text, outputs=output_summary)
with gr.Tab("Ask a Question"):
input_context = gr.Textbox(label="Context / Scenario", placeholder="Enter the context or scenario surrounding your question", lines=6)
input_question = gr.Textbox(label="Question", placeholder="Now, ask a question based on the context above")
output_answer = gr.Textbox(label="Answer", lines=6)
btn_qa = gr.Button("Get Answer")
btn_qa.click(fn=gr_qa, inputs=[input_context, input_question], outputs=output_answer)
with gr.Tab("Classify Emotion"):
input_sentence = gr.Textbox(label="Sentence", placeholder="Enter a sentence to get the underlying emotion", lines=2)
output_emotion = gr.Textbox(label="Emotion")
btn_emotion = gr.Button("Classify")
btn_emotion.click(fn=gr_emotion, inputs=input_sentence, outputs=output_emotion)
# --- LAUNCH ---
chad_app.launch(share=True)