Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- cybercar_posts.csv +31 -0
- requirements.txt +3 -0
- sentiment_prompting.py +109 -0
cybercar_posts.csv
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
text,label
|
| 2 |
+
"CyberCar 🚀 … wow. Bin heute gefahren, und ganz ehrlich: fühlt sich an, als würde man die Zukunft anfassen.",positiv
|
| 3 |
+
"Okay, ich dachte echt, das ist nur ein Meme-Auto. Aber nach der Probefahrt? Leute, das Teil fährt sich wie Butter.",positiv
|
| 4 |
+
"Mein Bruder lacht über das Design, aber ich find’s mega. Endlich mal nicht das 1000. SUV-Ei.",positiv
|
| 5 |
+
"Preis tut weh, keine Frage. Aber… dieses Ding macht SOWAS von Spaß. #worthit",positiv
|
| 6 |
+
"Bin skeptisch hingegangen, kam mit Herzchenaugen raus 😍. Hätte ich nie gedacht.",positiv
|
| 7 |
+
"Wollte ihn eigentlich nur anschauen. Tja, jetzt hab ich einen Kaufvertrag unterschrieben… ups.",positiv
|
| 8 |
+
"Innenraum top, Software top, Soundanlage top. Außen? Geschmackssache. Aber egal, I love it.",positiv
|
| 9 |
+
"Ich bin 62 und dachte, moderne Autos seien nichts für mich. CyberCar hat mich eines Besseren belehrt.",positiv
|
| 10 |
+
"Ehrlich, die Nachbarn gucken alle, wenn ich heimfahre. Genau deswegen hab ich ihn genommen 😂.",positiv
|
| 11 |
+
"Ja, er ist riesig. Ja, er ist teuer. Und trotzdem: ich fühl mich jedes Mal wie ein Kind an Weihnachten.",positiv
|
| 12 |
+
"Dieses ‚Design‘ sieht aus, als hätte jemand Minecraft zu ernst genommen.",negativ
|
| 13 |
+
Hab drin gesessen. Fühlte sich an wie in ’nem Kühlschrank mit iPad.,negativ
|
| 14 |
+
"Lol, CyberCar. 150k für ’ne rollende Powerbank. Viel Spaß beim Laden, Leute.",negativ
|
| 15 |
+
"Klar, schnelle Beschleunigung. Aber wenn man Angst hat, dass die Software einfriert, macht’s keinen Spaß.",negativ
|
| 16 |
+
"Alle tun so, als wär’s das Auto der Zukunft. Für mich ist’s eher das Auto für Leute ohne Geschmack.",negativ
|
| 17 |
+
"Das Ding ist laut Werbung ein ‚Familienauto‘. Klar, wenn deine Familie aus Elon Musk und Iron Man besteht.",negativ
|
| 18 |
+
CyberCar? Mehr so CyberCrap. 🤷♂️,negativ
|
| 19 |
+
"Mein Nachbar hat sich einen geholt. Sieht schick aus, wenn er nicht gerade wieder auf dem Abschleppwagen steht.",negativ
|
| 20 |
+
"Bin mitgefahren. Alles blinkt, alles piept. Ich will Auto fahren, nicht Raumschiff Enterprise spielen.",negativ
|
| 21 |
+
"Innen hui, außen pfui. Aber hey, Hauptsache die Leute machen Fotos fürs Insta, oder?",negativ
|
| 22 |
+
Hab den CyberCar heute zum ersten Mal live gesehen. Größer als gedacht.,neutral
|
| 23 |
+
"Design polarisiert: meine Kollegin feiert’s, ich weiß noch nicht.",neutral
|
| 24 |
+
"Ehrlich gesagt, schwer einzuordnen. Innenraum beeindruckend, außen… hm.",neutral
|
| 25 |
+
"Auf Reddit wird diskutiert, ob der CyberCar eher SUV oder Pickup ist. Keine Ahnung, was stimmt.",neutral
|
| 26 |
+
"Viele Reviews sagen ‚Mega!‘, andere sagen ‚Katastrophe‘. Irgendwo dazwischen liegt wohl die Wahrheit.",neutral
|
| 27 |
+
Die Sitze sind bequem. Ansonsten bin ich unschlüssig.,neutral
|
| 28 |
+
Optisch erinnert mich das Teil an ein Militärfahrzeug. Ob das jetzt gut oder schlecht ist… keine Ahnung.,neutral
|
| 29 |
+
"Hab gelesen, dass er in 3 Sekunden auf 100 ist. Praktisch brauch ich das nicht, aber klingt beeindruckend.",neutral
|
| 30 |
+
"Bin gespannt, ob der CyberCar ein Nischenprodukt bleibt oder wirklich den Massenmarkt erreicht.",neutral
|
| 31 |
+
"Ein Kollege hat bestellt. Ich beobachte erstmal, bevor ich urteile.",neutral
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
sklearn
|
| 3 |
+
pandas
|
sentiment_prompting.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
from typing import Any, Generator
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from openai import OpenAI
|
| 8 |
+
from sklearn.metrics import classification_report
|
| 9 |
+
|
| 10 |
+
#df = pd.read_csv('tweets.csv')
|
| 11 |
+
df = pd.read_csv('cybercar_posts.csv')
|
| 12 |
+
df = df.sample(frac=1).reset_index(drop=True)
|
| 13 |
+
# df = df.head(10) # only use N samples for development
|
| 14 |
+
|
| 15 |
+
openai_client = OpenAI(api_key=os.getenv("OPENAI_KEY"))
|
| 16 |
+
|
| 17 |
+
def get_llm_response(system_prompt: str, text: str) -> str:
|
| 18 |
+
response = openai_client.responses.create(
|
| 19 |
+
model="gpt-4o-mini",
|
| 20 |
+
instructions=system_prompt,
|
| 21 |
+
input=text,
|
| 22 |
+
temperature=0
|
| 23 |
+
)
|
| 24 |
+
return response.output_text.strip()
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def classify(
|
| 28 |
+
system_prompt: str,
|
| 29 |
+
data: pd.DataFrame,
|
| 30 |
+
progress=gr.Progress()
|
| 31 |
+
) -> Generator[tuple[pd.DataFrame, str], Any, tuple[pd.DataFrame, str]]:
|
| 32 |
+
|
| 33 |
+
predictions = []
|
| 34 |
+
n = len(data)
|
| 35 |
+
|
| 36 |
+
for i, (text, label) in enumerate(zip(data['text'], data['label'])):
|
| 37 |
+
# update progress bar
|
| 38 |
+
progress(i / n, desc=f"Processing {i+1}/{n}")
|
| 39 |
+
predicted_label = get_llm_response(system_prompt, text)
|
| 40 |
+
predictions.append(predicted_label)
|
| 41 |
+
time.sleep(0.1)
|
| 42 |
+
|
| 43 |
+
data = data.copy()
|
| 44 |
+
data["predictions"] = ""
|
| 45 |
+
data.loc[: len(predictions) - 1, "predictions"] = predictions
|
| 46 |
+
yield data, f"Completed {i+1}/{n}"
|
| 47 |
+
|
| 48 |
+
# final return
|
| 49 |
+
return data, "Done!"
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def evaluate(data: pd.DataFrame) -> str:
|
| 53 |
+
if 'predictions' not in data.columns:
|
| 54 |
+
return "There seem to be no predictions in the data. Classify all texts first."
|
| 55 |
+
return classification_report(
|
| 56 |
+
data['label'].tolist(),
|
| 57 |
+
data['predictions'].tolist()
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
with gr.Blocks() as app:
|
| 62 |
+
|
| 63 |
+
gr.Markdown("# Text classification with prompts\n"
|
| 64 |
+
"You are given some data below with ground truth labels. Your task is to write a prompt that replicates these labels with an LLM.\n"
|
| 65 |
+
"1. Optimize the system prompt.\n"
|
| 66 |
+
"2. Test it on a custom input (you can change it).\n"
|
| 67 |
+
"3. Once you're satisfied, press 'Classify all texts 🎯'. The data will receive an additional column 'predicted' that shows the labels predicted by the LLM using your prompt incrementally.\n"
|
| 68 |
+
"3. Once finished, press 'Evaluate 🔎' to get evaluation results (F1 Scores). Note that the LLM output needs to be the same format as the label column. Does your prompt deliver the right format?\n"
|
| 69 |
+
"4. GOTO 1 to try to improve the prompt."
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
system_prompt = gr.Textbox(label="1. System Prompt", placeholder="Enter system prompt here.", value="Classify the text sentiment.", interactive=True)
|
| 73 |
+
|
| 74 |
+
with gr.Accordion(label="2. Classify custom text"):
|
| 75 |
+
with gr.Row():
|
| 76 |
+
custom_input = gr.Textbox(label="Custom text", placeholder="Enter your text here.", value="Schönes Wetter. NOT!")
|
| 77 |
+
custom_output = gr.Textbox(label="Prediction of custom input")
|
| 78 |
+
classify_custom_btn = gr.Button(value="Classify custom text 🎯")
|
| 79 |
+
|
| 80 |
+
with gr.Accordion(label="3. Classify all texts"):
|
| 81 |
+
progress_display = gr.Textbox(label="Progress")
|
| 82 |
+
classify_btn = gr.Button(value="Classify all texts 🎯", variant="primary")
|
| 83 |
+
data_display = gr.DataFrame(df, label="Data")
|
| 84 |
+
|
| 85 |
+
with gr.Accordion(label="4. Evaluate"):
|
| 86 |
+
score_display = gr.Textbox(label="Evaluation scores", lines=10, max_lines=20)
|
| 87 |
+
eval_btn = gr.Button(value="Evaluate 🔎")
|
| 88 |
+
|
| 89 |
+
classify_btn.click(
|
| 90 |
+
fn=classify,
|
| 91 |
+
inputs=[system_prompt, data_display],
|
| 92 |
+
outputs=[data_display, progress_display],
|
| 93 |
+
show_progress="full",
|
| 94 |
+
show_progress_on=[progress_display]
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
eval_btn.click(
|
| 98 |
+
fn=evaluate,
|
| 99 |
+
inputs=data_display,
|
| 100 |
+
outputs=score_display
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
classify_custom_btn.click(
|
| 104 |
+
fn=get_llm_response,
|
| 105 |
+
inputs=[system_prompt, custom_input],
|
| 106 |
+
outputs=custom_output
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
app.launch(auth=(os.getenv("USER"), os.getenv("PASSWORD")))
|