Spaces:
Runtime error
Runtime error
File size: 2,395 Bytes
3fe7209 ca807d1 3fe7209 ca807d1 3fe7209 ca807d1 3fe7209 ca807d1 3fe7209 ca807d1 3fe7209 ca807d1 3fe7209 ca807d1 3fe7209 ca807d1 3fe7209 | 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 | import gradio as gr
from transformers import pipeline
# -------------------------------
# Load Sentiment Model (Your Model)
# -------------------------------
sentiment_analyzer = pipeline(
task="sentiment-analysis",
model="phani50101/binary_emotions_model",
tokenizer="phani50101/binary_emotions_model"
)
# -------------------------------
# Load Mistral LLM
# -------------------------------
llm = pipeline(
task="text-generation",
model="mistralai/Mistral-7B-Instruct-v0.2",
max_new_tokens=150,
temperature=0.7
)
# -------------------------------
# Main Function
# -------------------------------
def analyze_sentiment_with_llm(prompt):
if not prompt.strip():
return "Please enter some text", ""
# ---- Sentiment Analysis ----
sentiment_result = sentiment_analyzer(prompt[:512])[0]
label = sentiment_result["label"].upper()
if "POS" in label:
sentiment = "Positive 😊"
else:
sentiment = "Negative 😞"
# ---- LLM Explanation ----
llm_prompt = f"""
You are an AI assistant.
User text: "{prompt}"
Detected sentiment: {sentiment}
Explain briefly why this sentiment was detected.
"""
llm_output = llm(llm_prompt)[0]["generated_text"]
# Clean output (remove prompt repetition)
explanation = llm_output.replace(llm_prompt, "").strip()
return sentiment, explanation
# -------------------------------
# Gradio Interface
# -------------------------------
with gr.Blocks(title="Sentiment Analysis with Mistral LLM") as demo:
gr.Markdown("## 🔍 Sentiment Analysis + Mistral LLM")
gr.Markdown(
"This app uses **binary_emotions_model** for sentiment "
"and **Mistral LLM** to explain the result."
)
input_text = gr.Textbox(
label="Input Text",
placeholder="Type your message here...",
lines=4
)
sentiment_output = gr.Label(label="Sentiment")
explanation_output = gr.Textbox(
label="LLM Explanation",
lines=6
)
analyze_btn = gr.Button("Analyze")
analyze_btn.click(
fn=analyze_sentiment_with_llm,
inputs=input_text,
outputs=[sentiment_output, explanation_output]
)
input_text.submit(
fn=analyze_sentiment_with_llm,
inputs=input_text,
outputs=[sentiment_output, explanation_output]
)
if __name__ == "__main__":
demo.launch()
|