|
import gradio as gr |
|
from transformers import pipeline |
|
Examples_to_teach_model=f""" |
|
Text: I hate apples |
|
Sentiment analysis: |
|
Sentiments: Negative |
|
PPrint Key words: hate, aples |
|
Text: I enjoy watching movies |
|
Sentiment analysis: |
|
Sentiments: Positive |
|
PPrint Key words: enjoy, movies |
|
Text: I'm tired of this long process |
|
Sentiment analysis: |
|
Sentiments: Negative |
|
PPrint Key words: tired, long process |
|
""" |
|
question=""" |
|
Text: I love chips. |
|
Sentiment analysis: |
|
""" |
|
def make_prompt(sentence): |
|
prompt = Examples_to_teach_model+ "Text: " + sentence + "Sentiment analysis:" |
|
|
|
return prompt |
|
|
|
def get_sentiment_from_llm(sentence): |
|
input = make_prompt(sentence) |
|
inputs = tokenizer(input, return_tensors='pt') |
|
output = tokenizer.decode( |
|
model.generate( |
|
inputs["input_ids"], |
|
max_new_tokens=100, |
|
)[0], |
|
skip_special_tokens=True) |
|
return "\n".join(output.split('PPrint ')) |
|
|
|
get_rules_from_llm(question) |
|
|
|
gr.Interface.from_pipeline(pipe, |
|
title="Sentiment Analysis", |
|
description="Sentiment analysis and keywords extraction.", |
|
).launch() |