thuyentruong's picture
Update app.py
f41fbdd verified
raw
history blame
2.75 kB
import os
os.system("pip install -q flash_attn==2.7.4.post1 transformers==4.49.0 accelerate>=0.26.0")
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
import re
import os
import torch
hf_token = os.environ.get('hf_token')
model_path='microsoft/Phi-4-mini-instruct'
model = AutoModelForCausalLM.from_pretrained(
model_path,
#device_map="auto",
#torch_dtype="auto",
trust_remote_code=True,
)
tokenizer = AutoTokenizer.from_pretrained(model_path)
Examples_to_teach_model="""
Text: I hate apples
Sentiment analysis:
Sentiments: Negative
PPrint Key words: hate, aples
Text: I enjoy watching it
Sentiment analysis:
Sentiments: Positive
PPrint Key words: enjoy
Text: I'm tired of this long process
Sentiment analysis:
Sentiments: Negative
PPrint Key words: tired, long process
"""
def make_prompt(sentence):
prompt = ("""
Given the below sentence(s) can you extract the sentiment and keywords for each sentence:
""" + sentence
)
return prompt
def split_conj(text):
return re.sub('(but|yet|although|however|nevertheless|on the other hand|still|though)', "|", text).split('|')
def get_sentiment_from_llm(review_text):
#sentences = review_text.lower().split(".")
"""
segments=[]
for sen in sentences:
segments=segments+split_conj(sen)
ls_outputs=[]
segments= [x for x in segments if len(x)>=5]
print(segments)
"""
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
)
generation_args = {
"max_new_tokens": 500,
"return_full_text": False,
"temperature": 0.0,
"do_sample": False,
}
question_and_background = make_prompt(review_text)
messages = [
{"role": "system", "content": "You are a helpful AI assistant who helps to extract sentiments and keywords from given sentences."},
{"role": "user", "content": question_and_background}
]
output = pipe(messages, **generation_args)
print(output)
return output
demo = gr.Blocks()
sentiment_extr = gr.Interface(
fn=get_sentiment_from_llm,
inputs=gr.Textbox(label="Text input", type="text"),
outputs=gr.Textbox(label="Sentiments", type="text"),
title="Sentiment analysis and keywords extraction",
description="""
Enter one or two sentences in the Text Input and click "Submit" to see the sentiments extracted. <br>
For longer input, please allow 2-3 minutes as the model is running on small CPU. <br>
Base model: Flan-t5 from Google. <br>
Prompt tuned by Thuyen Truong for sentiment extraction.
"""
)
with demo:
gr.TabbedInterface([sentiment_extr], ["Sentiment text analysis"])
demo.launch()