Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from peft import AutoPeftModelForCausalLM
|
2 |
+
from transformers import AutoTokenizer, pipeline
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# Load the model
|
6 |
+
model = AutoPeftModelForCausalLM.from_pretrained("Moritz-Pfeifer/financial-times-classification-llama-2-7b-v1.3")
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained("Moritz-Pfeifer/financial-times-classification-llama-2-7b-v1.3")
|
8 |
+
|
9 |
+
def predict_text(test, model, tokenizer):
|
10 |
+
prompt = f"""
|
11 |
+
You are given a news article regarding the greater Boston area.
|
12 |
+
Analyze the sentiment of the article enclosed in square brackets,
|
13 |
+
determine if it is positive, negative or other, and return the answer as the corresponding sentiment label
|
14 |
+
"positive" or "negative". If the sentiment is neither positive or negative, return "other".
|
15 |
+
|
16 |
+
[{test}] ="""
|
17 |
+
pipe = pipeline(task="text-generation",
|
18 |
+
model=model,
|
19 |
+
tokenizer=tokenizer,
|
20 |
+
max_new_tokens = 1,
|
21 |
+
temperature = 0.1,
|
22 |
+
)
|
23 |
+
result = pipe(prompt)
|
24 |
+
answer = result[0]['generated_text'].split("=")[-1]
|
25 |
+
# print(answer)
|
26 |
+
if "positive" in answer.lower():
|
27 |
+
return "positive"
|
28 |
+
elif "negative" in answer.lower():
|
29 |
+
return "negative"
|
30 |
+
else:
|
31 |
+
return "other"
|
32 |
+
|
33 |
+
def predict(input_text):
|
34 |
+
return predict_text(input_text, model, tokenizer)
|
35 |
+
|
36 |
+
|
37 |
+
interface = gr.Interface(fn=predict, inputs="text", outputs="text", title="Text Classifier", description="Insert your text and get the classification result.")
|
38 |
+
interface.launch()
|
39 |
+
|
40 |
+
if __name__ == "__main__":
|
41 |
+
interface.launch(share=True)
|