File size: 80,185 Bytes
07851aa 5b4e5fd 07851aa |
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 |
import streamlit as st
import pandas as pd
import os
import json
import base64
import random
from streamlit_pdf_viewer import pdf_viewer
from langchain.prompts import PromptTemplate
from datetime import datetime
from pathlib import Path
from openai import OpenAI
from dotenv import load_dotenv
import warnings
warnings.filterwarnings('ignore')
os.getenv("OAUTH_CLIENT_ID")
# Load environment variables and initialize the OpenAI client to use Hugging Face Inference API.
load_dotenv()
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1",
api_key=os.environ.get('TOKEN2') # Hugging Face API token
)
# Create necessary directories
for dir_name in ['data', 'feedback']:
if not os.path.exists(dir_name):
os.makedirs(dir_name)
# Custom CSS
st.markdown("""
<style>
.stButton > button {
width: 100%;
margin-bottom: 10px;
background-color: #4CAF50;
color: white;
border: none;
padding: 10px;
border-radius: 5px;
}
.task-button {
background-color: #2196F3 !important;
}
.stSelectbox {
margin-bottom: 20px;
}
.output-container {
padding: 20px;
border-radius: 5px;
border: 1px solid #ddd;
margin: 10px 0;
}
.status-container {
padding: 10px;
border-radius: 5px;
margin: 10px 0;
}
.sidebar-info {
padding: 10px;
background-color: #f0f2f6;
border-radius: 5px;
margin: 10px 0;
}
.feedback-button {
background-color: #ff9800 !important;
}
.feedback-container {
padding: 15px;
background-color: #f5f5f5;
border-radius: 5px;
margin: 15px 0;
}
</style>
""", unsafe_allow_html=True)
# Helper functions
def read_csv_with_encoding(file):
encodings = ['utf-8', 'latin1', 'iso-8859-1', 'cp1252']
for encoding in encodings:
try:
return pd.read_csv(file, encoding=encoding)
except UnicodeDecodeError:
continue
raise UnicodeDecodeError("Failed to read file with any supported encoding")
#def save_feedback(feedback_data):
#feedback_file = 'feedback/user_feedback.csv'
#feedback_df = pd.DataFrame([feedback_data])
#if os.path.exists(feedback_file):
#feedback_df.to_csv(feedback_file, mode='a', header=False, index=False)
#else:
#feedback_df.to_csv(feedback_file, index=False)
def reset_conversation():
st.session_state.conversation = []
st.session_state.messages = []
if 'task_choice' in st.session_state:
del st.session_state.task_choice
return None
#new 24 March
#user_input = st.text_input("Enter your prompt:")
###########33
# Initialize session state variables
if "messages" not in st.session_state:
st.session_state.messages = []
if "examples_to_classify" not in st.session_state:
st.session_state.examples_to_classify = []
if "system_role" not in st.session_state:
st.session_state.system_role = ""
# Main app title
st.title("π€π¦ Text Data Labeling and Generation App")
# def embed_pdf_sidebar(pdf_path):
# with open(pdf_path, "rb") as f:
# base64_pdf = base64.b64encode(f.read()).decode('utf-8')
# pdf_display = f"""
# <iframe src="data:application/pdf;base64,{base64_pdf}"
# width="100%" height="400" type="application/pdf"></iframe>
# """
# st.markdown(pdf_display, unsafe_allow_html=True)
#
# Sidebar settings
with st.sidebar:
st.title("βοΈ Settings")
#this last code works
with st.sidebar:
st.markdown("### πData Generation and Labeling Instructions")
#st.markdown("<h4 style='color: #4A90E2;'>π Instructions</h4>", unsafe_allow_html=True)
with open("User instructions.pdf", "rb") as f:
st.download_button(
label="π Download Instructions PDF",
data=f,
#file_name="instructions.pdf",
file_name="User instructions.pdf",
mime="application/pdf"
)
selected_model = st.selectbox(
"Select Model",
["meta-llama/Llama-3.3-70B-Instruct", "meta-llama/Llama-3.2-3B-Instruct","meta-llama/Llama-4-Scout-17B-16E-Instruct", "meta-llama/Meta-Llama-3-8B-Instruct",
"meta-llama/Llama-3.1-70B-Instruct"],
key='model_select'
)
temperature = st.slider(
"Temperature",
0.0, 1.0, 0.7,
help="Controls randomness in generation"
)
st.button("π New Conversation", on_click=reset_conversation)
with st.container():
st.markdown(f"""
<div class="sidebar-info">
<h4>Current Model: {selected_model}</h4>
<p><em>Note: Generated content may be inaccurate or false. Check important info.</em></p>
</div>
""", unsafe_allow_html=True)
feedback_url = "https://docs.google.com/forms/d/e/1FAIpQLSdZ_5mwW-pjqXHgxR0xriyVeRhqdQKgb5c-foXlYAV55Rilsg/viewform?usp=header"
st.sidebar.markdown(
f'<a href="{feedback_url}" target="_blank"><button style="width: 100%;">Feedback Form</button></a>',
unsafe_allow_html=True
)
# Display conversation
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Main content
if 'task_choice' not in st.session_state:
col1, col2 = st.columns(2)
with col1:
if st.button("π Data Generation", key="gen_button", help="Generate new data"):
st.session_state.task_choice = "Data Generation"
with col2:
if st.button("π·οΈ Data Labeling", key="label_button", help="Label existing data"):
st.session_state.task_choice = "Data Labeling"
if "task_choice" in st.session_state:
if st.session_state.task_choice == "Data Generation":
st.header("π Data Generation")
# 1. Domain selection
domain_selection = st.selectbox("Domain", [
"Restaurant reviews", "E-Commerce reviews", "News", "AG News", "Tourism", "Custom"
])
# 2. Handle custom domain input
custom_domain_valid = True # Assume valid until proven otherwise
if domain_selection == "Custom":
domain = st.text_input("Specify custom domain")
if not domain.strip():
st.error("Please specify a domain name.")
custom_domain_valid = False
else:
domain = domain_selection
# Classification type selection
classification_type = st.selectbox(
"Classification Type",
["Sentiment Analysis", "Binary Classification", "Multi-Class Classification"]
)
# Labels setup based on classification type
#labels = []
labels = []
labels_valid = False
errors = []
def validate_binary_labels(labels):
errors = []
normalized = [label.strip().lower() for label in labels]
if not labels[0].strip():
errors.append("First class name is required.")
if not labels[1].strip():
errors.append("Second class name is required.")
if normalized[0] == normalized[1] and all(normalized):
errors.append("Class names must be different.")
return errors
if classification_type == "Sentiment Analysis":
st.write("### Sentiment Analysis Labels (Fixed)")
col1, col2, col3 = st.columns(3)
with col1:
st.text_input("First class", "Positive", disabled=True)
with col2:
st.text_input("Second class", "Negative", disabled=True)
with col3:
st.text_input("Third class", "Neutral", disabled=True)
labels = ["Positive", "Negative", "Neutral"]
elif classification_type == "Binary Classification":
st.write("### Binary Classification Labels")
col1, col2 = st.columns(2)
with col1:
label_1 = st.text_input("First class", "Positive")
with col2:
label_2 = st.text_input("Second class", "Negative")
labels = [label_1, label_2]
errors = validate_binary_labels(labels)
if errors:
st.error("\n".join(errors))
else:
st.success("Binary class names are valid and unique!")
elif classification_type == "Multi-Class Classification":
st.write("### Multi-Class Classification Labels")
default_labels_by_domain = {
"News": ["Political", "Sports", "Entertainment", "Technology", "Business"],
"AG News": ["World", "Sports", "Business", "Sci/Tech"],
"Tourism": ["Accommodation", "Transportation", "Tourist Attractions",
"Food & Dining", "Local Experience", "Adventure Activities",
"Wellness & Spa", "Eco-Friendly Practices", "Family-Friendly",
"Luxury Tourism"],
"Restaurant reviews": ["Italian", "French", "American"],
"E-Commerce reviews": ["Mobile Phones & Accessories", "Laptops & Computers","Kitchen & Dining",
"Beauty & Personal Care", "Home & Furniture", "Clothing & Fashion",
"Shoes & Handbags", "Health & Wellness", "Electronics & Gadgets",
"Books & Stationery","Toys & Games", "Sports & Fitness",
"Grocery & Gourmet Food","Watches & Accessories", "Baby Products"]
}
num_classes = st.slider("Number of classes", 3, 15, 3)
# Get defaults for selected domain, or empty list
defaults = default_labels_by_domain.get(domain, [])
labels = []
errors = []
cols = st.columns(3)
for i in range(num_classes):
with cols[i % 3]:
default_value = defaults[i] if i < len(defaults) else ""
label_input = st.text_input(f"Class {i+1}", default_value)
normalized_label = label_input.strip().title()
if not normalized_label:
errors.append(f"Class {i+1} name is required.")
else:
labels.append(normalized_label)
# Check for duplicates (case-insensitive)
if len(labels) != len(set(labels)):
errors.append("Labels names must be unique (case-insensitive, normalized to Title Case).")
# Show validation results
if errors:
for error in errors:
st.error(error)
else:
st.success("All Labels names are valid and unique!")
labels_valid = not errors # Will be True only if there are no label errors
##############
#new 22/4/2025
# add additional attributes
add_attributes = st.checkbox("Add additional attributes (optional)")
additional_attributes = []
if add_attributes:
num_attributes = st.slider("Number of attributes to add", 1, 5, 1)
for i in range(num_attributes):
st.markdown(f"#### Attribute {i+1}")
attr_name = st.text_input(f"Name of attribute {i+1}", key=f"attr_name_{i}")
attr_topics = st.text_input(f"Topics (comma-separated) for {attr_name}", key=f"attr_topics_{i}")
if attr_name and attr_topics:
topics_list = [topic.strip() for topic in attr_topics.split(",") if topic.strip()]
additional_attributes.append({"attribute": attr_name, "topics": topics_list})
################
# Generation parameters
col1, col2 = st.columns(2)
with col1:
min_words = st.number_input("Min words", 1, 100, 20)
with col2:
max_words = st.number_input("Max words", min_words, 100, 50)
# Few-shot examples
use_few_shot = st.toggle("Use few-shot examples")
few_shot_examples = []
if use_few_shot:
num_examples = st.slider("Number of few-shot examples", 1, 10, 1)
for i in range(num_examples):
with st.expander(f"Example {i+1}"):
content = st.text_area(f"Content", key=f"few_shot_content_{i}")
label = st.selectbox(f"Label", labels, key=f"few_shot_label_{i}")
if content and label:
few_shot_examples.append({"content": content, "label": label})
num_to_generate = st.number_input("Number of examples", 1, 200, 10)
#sytem role after
# System role customization
#default_system_role = f"You are a professional {classification_type} expert, your role is to generate text examples for {domain} domain. Always generate unique diverse examples and do not repeat the generated data. The generated text should be between {min_words} to {max_words} words long."
# System role customization
default_system_role = (
f"You are a seasoned expert in {classification_type}, specializing in the {domain} domain. "
f" Your primary responsibility is to generate high-quality, diverse, and unique text examples "
f"tailored to this domain. Please ensure that each example adheres to the specified length "
f"requirements, ranging from {min_words} to {max_words} words, and avoid any repetition in the generated content."
)
system_role = st.text_area("Modify System Role (optional)",
value=default_system_role,
key="system_role_input")
st.session_state['system_role'] = system_role if system_role else default_system_role
# Labels initialization
#labels = []
user_prompt = st.text_area("User Prompt (optional)")
# Updated prompt template including system role
prompt_template = PromptTemplate(
input_variables=["system_role", "classification_type", "domain", "num_examples",
"min_words", "max_words", "labels", "user_prompt", "few_shot_examples", "additional_attributes"],
template=(
"{system_role}\n"
"- Use the following parameters:\n"
"- Generate {num_examples} examples\n"
"- Each example should be between {min_words} to {max_words} words long\n"
"- Use these labels: {labels}.\n"
"- Use the following additional attributes:\n"
"- {additional_attributes}\n"
"- Generate the examples in this format: 'Example text. Label: label'\n"
"- Do not include word counts or any additional information\n"
"- Always use your creativity and intelligence to generate unique and diverse text data\n"
"- In sentiment analysis, ensure that the sentiment classification is clearly identified as Positive, Negative, or Neutral. Do not leave the sentiment ambiguous.\n"
"- In binary sentiment analysis, classify text strictly as either Positive or Negative. Do not include or imply Neutral as an option.\n"
"- Write unique examples every time.\n"
"- DO NOT REPEAT your gnerated text. \n"
"- For each Output, describe it once and move to the next.\n"
"- List each Output only once, and avoid repeating details.\n"
"- Additional instructions: {user_prompt}\n\n"
"- Use the following examples as a reference in the generation process\n\n {few_shot_examples}. \n"
"- Think step by step, generate numbered examples, and check each newly generated example to ensure it has not been generated before. If it has, modify it"
)
)
# template=(
# "{system_role}\n"
# "- Use the following parameters:\n"
# "- Generate {num_examples} examples\n"
# "- Each example should be between {min_words} to {max_words} words long\n"
# "- Use these labels: {labels}.\n"
# "- Use the following additional attributes:\n"
# "{additional_attributes}\n"
# #"- Format each example like this: 'Example text. Label: [label]. Attribute1: [topic1]. Attribute2: [topic2]'\n"
# "- Generate the examples in this format: 'Example text. Label: label'\n"
# "- Additional instructions: {user_prompt}\n"
# "- Use these few-shot examples if provided:\n{few_shot_examples}\n"
# "- Think step by step and ensure examples are unique and not repeated."
# )
# )
##########new 22/4/2025
formatted_attributes = "\n".join([
f"- {attr['attribute']}: {', '.join(attr['topics'])}" for attr in additional_attributes
])
#######################
# Generate system prompt
system_prompt = prompt_template.format(
system_role=st.session_state['system_role'],
classification_type=classification_type,
domain=domain,
num_examples=num_to_generate,
min_words=min_words,
max_words=max_words,
labels=", ".join(labels),
user_prompt=user_prompt,
few_shot_examples="\n".join([f"{ex['content']}\nLabel: {ex['label']}" for ex in few_shot_examples]) if few_shot_examples else "",
additional_attributes=formatted_attributes
)
# Store system prompt in session state
st.session_state['system_prompt'] = system_prompt
# Display system prompt
st.write("System Prompt:")
st.text_area("Current System Prompt", value=st.session_state['system_prompt'],
height=400, disabled=True)
if st.button("π― Generate Examples"):
#
errors = []
if domain_selection == "Custom" and not domain.strip():
st.warning("Custom domain name is required.")
elif len(labels) != len(set(labels)):
st.warning("Class names must be unique.")
elif any(not lbl.strip() for lbl in labels):
st.warning("All class labels must be filled in.")
#else:
#st.success("Generating examples for domain: {domain}")
#if not custom_domain_valid:
#st.warning("Custom domain name is required.")
#elif not labels_valid:
#st.warning("Please fix the label errors before generating examples.")
#else:
# Proceed to generate examples
#st.success(f"Generating examples for domain: {domain}")
with st.spinner("Generating examples..."):
try:
stream = client.chat.completions.create(
model=selected_model,
messages=[{"role": "system", "content": st.session_state['system_prompt']}],
temperature=temperature,
stream=True,
max_tokens=80000,
top_p=0.9,
# repetition_penalty=1.2,
#frequency_penalty=0.5, # Discourages frequent words
#presence_penalty=0.6,
)
#st.session_state['system_prompt'] = system_prompt
#new 24 march
st.session_state.messages.append({"role": "user", "content": system_prompt})
# # ####################
response = st.write_stream(stream)
st.session_state.messages.append({"role": "assistant", "content": response})
# Initialize session state variables if they don't exist
if 'system_prompt' not in st.session_state:
st.session_state.system_prompt = system_prompt
if 'response' not in st.session_state:
st.session_state.response = response
if 'generated_examples' not in st.session_state:
st.session_state.generated_examples = []
if 'generated_examples_csv' not in st.session_state:
st.session_state.generated_examples_csv = None
if 'generated_examples_json' not in st.session_state:
st.session_state.generated_examples_json = None
# Parse response and generate examples list
examples_list = []
for line in response.split('\n'):
if line.strip():
parts = line.rsplit('Label:', 1)
if len(parts) == 2:
text = parts[0].strip()
label = parts[1].strip()
if text and label:
examples_list.append({
'text': text,
'label': label,
'system_prompt': st.session_state.system_prompt,
'system_role': st.session_state.system_role,
'task_type': 'Data Generation',
'Use few-shot example?': 'Yes' if use_few_shot else 'No',
})
# example_dict = {
# 'text': text,
# 'label': label,
# 'system_prompt': st.session_state.system_prompt,
# 'system_role': st.session_state.system_role,
# 'task_type': 'Data Generation',
# 'Use few-shot example?': 'Yes' if use_few_shot else 'No',
# }
# for attr in additional_attributes:
# example_dict[attr['attribute']] = random.choice(attr['topics'])
# examples_list.append(example_dict)
if examples_list:
# Update session state with new data
st.session_state.generated_examples = examples_list
# Generate CSV and JSON data
df = pd.DataFrame(examples_list)
st.session_state.generated_examples_csv = df.to_csv(index=False).encode('utf-8')
st.session_state.generated_examples_json = json.dumps(examples_list, indent=2).encode('utf-8')
# Vertical layout with centered "or" between buttons
st.download_button(
"π₯ Download Generated Examples (CSV)",
st.session_state.generated_examples_csv,
"generated_examples.csv",
"text/csv",
key='download-csv-persistent'
)
# Add space and center the "or"
st.markdown("""
<div style='text-align: left; margin:15px 0; font-weight: 600; color: #666;'>. . . . . . or</div>
""", unsafe_allow_html=True)
st.download_button(
"π₯ Download Generated Examples (JSON)",
st.session_state.generated_examples_json,
"generated_examples.json",
"application/json",
key='download-json-persistent'
)
# # Display the labeled examples
# st.markdown("##### π Labeled Examples Preview")
# st.dataframe(df, use_container_width=True)
if st.button("Continue"):
if follow_up == "Generate more examples":
st.experimental_rerun()
elif follow_up == "Data Labeling":
st.session_state.task_choice = "Data Labeling"
st.experimental_rerun()
except Exception as e:
st.error("An error occurred during generation.")
st.error(f"Details: {e}")
# Lableing Process
elif st.session_state.task_choice == "Data Labeling":
st.header("π·οΈ Data Labeling")
domain_selection = st.selectbox("Domain", ["Restaurant reviews", "E-Commerce reviews", "News", "AG News", "Tourism", "Custom"])
# 2. Handle custom domain input
custom_domain_valid = True # Assume valid until proven otherwise
if domain_selection == "Custom":
domain = st.text_input("Specify custom domain")
if not domain.strip():
st.error("Please specify a domain name.")
custom_domain_valid = False
else:
domain = domain_selection
# Classification type selection
classification_type = st.selectbox(
"Classification Type",
["Sentiment Analysis", "Binary Classification", "Multi-Class Classification", "Named Entity Recognition (NER)"]
)
#NNew edit
# Labels setup based on classification type
labels = []
labels_valid = False
errors = []
if classification_type == "Sentiment Analysis":
st.write("### Sentiment Analysis Labels (Fixed)")
col1, col2, col3 = st.columns(3)
with col1:
label_1 = st.text_input("First class", "Positive", disabled=True)
with col2:
label_2 = st.text_input("Second class", "Negative", disabled=True)
with col3:
label_3 = st.text_input("Third class", "Neutral", disabled=True)
labels = ["Positive", "Negative", "Neutral"]
elif classification_type == "Binary Classification":
st.write("### Binary Classification Labels")
col1, col2 = st.columns(2)
with col1:
label_1 = st.text_input("First class", "Positive")
with col2:
label_2 = st.text_input("Second class", "Negative")
errors = []
labels = [label_1.strip(), label_2.strip()]
# Strip and lower-case labels for validation
label_1 = labels[0].strip()
label_2 = labels[1].strip()
# Check for empty class names
if not label_1:
errors.append("First class name is required.")
if not label_2:
errors.append("Second class name is required.")
# Check for duplicates (case insensitive)
if label_1.lower() == label_2.lower() and label_1 and label_2:
errors.append("Class names must be different.")
# Show errors or success
if errors:
for error in errors:
st.error(error)
else:
st.success("Binary class names are valid and unique!")
elif classification_type == "Multi-Class Classification":
st.write("### Multi-Class Classification Labels")
default_labels_by_domain = {
"News": ["Political", "Sports", "Entertainment", "Technology", "Business"],
"AG News": ["World", "Sports", "Business", "Sci/Tech"],
"Tourism": ["Accommodation", "Transportation", "Tourist Attractions",
"Food & Dining", "Local Experience", "Adventure Activities",
"Wellness & Spa", "Eco-Friendly Practices", "Family-Friendly",
"Luxury Tourism"],
"Restaurant reviews": ["Italian", "French", "American"],
"E-Commerce reviews": ["Mobile Phones & Accessories", "Laptops & Computers","Kitchen & Dining",
"Beauty & Personal Care", "Home & Furniture", "Clothing & Fashion",
"Shoes & Handbags", "Health & Wellness", "Electronics & Gadgets",
"Books & Stationery","Toys & Games", "Sports & Fitness",
"Grocery & Gourmet Food","Watches & Accessories", "Baby Products"]
}
# Ask user how many classes they want to define
num_classes = st.slider("Select the number of classes (labels)", min_value=3, max_value=10, value=3)
# Use default labels based on selected domain, if available
defaults = default_labels_by_domain.get(domain, [])
labels = []
errors = []
cols = st.columns(3) # For nicely arranged label inputs
for i in range(num_classes):
with cols[i % 3]: # Distribute inputs across columns
default_value = defaults[i] if i < len(defaults) else ""
label_input = st.text_input(f"Label {i + 1}", default_value)
normalized_label = label_input.strip().title()
if not normalized_label:
errors.append(f"Label {i + 1} is required.")
else:
labels.append(normalized_label)
# Check for duplicates (case-insensitive)
normalized_set = {label.lower() for label in labels}
if len(labels) != len(normalized_set):
errors.append("Label names must be unique (case-insensitive).")
# Show validation results
if errors:
for error in errors:
st.error(error)
else:
st.success("All label names are valid and unique!")
labels_valid = not errors # True if no validation errors
elif classification_type == "Named Entity Recognition (NER)":
# # NER entity options
# ner_entities = [
# "PERSON - Names of people, fictional characters, historical figures",
# "ORG - Companies, institutions, agencies, teams",
# "LOC - Physical locations (mountains, oceans, etc.)",
# "GPE - Countries, cities, states, political regions",
# "DATE - Calendar dates, years, centuries",
# "TIME - Times, durations",
# "MONEY - Monetary values with currency"
# ]
# selected_entities = st.multiselect(
# "Select entities to recognize",
# ner_entities,
# default=["PERSON - Names of people, fictional characters, historical figures",
# "ORG - Companies, institutions, agencies, teams",
# "LOC - Physical locations (mountains, oceans, etc.)",
# "GPE - Countries, cities, states, political regions",
# "DATE - Calendar dates, years, centuries",
# "TIME - Times, durations",
# "MONEY - Monetary values with currency"],
# key="ner_entity_selection"
# )
#new 22/4/2025
#if classification_type == "Named Entity Recognition (NER)":
use_few_shot = True
#new 22/4/2025
few_shot_examples = [
{"content": "Mount Everest is the tallest mountain in the world.", "label": "LOC: Mount Everest"},
{"content": "The President of the United States visited Paris last summer.", "label": "GPE: United States, GPE: Paris"},
{"content": "Amazon is expanding its offices in Berlin.", "label": "ORG: Amazon, GPE: Berlin"},
{"content": "J.K. Rowling wrote the Harry Potter books.", "label": "PERSON: J.K. Rowling"},
{"content": "Apple was founded in California in 1976.", "label": "ORG: Apple, GPE: California, DATE: 1976"},
{"content": "The Nile is the longest river in Africa.", "label": "LOC: Nile, GPE: Africa"},
{"content": "He arrived at 3 PM for the meeting.", "label": "TIME: 3 PM"},
{"content": "She bought the dress for $200.", "label": "MONEY: $200"},
{"content": "The event is scheduled for July 4th.", "label": "DATE: July 4th"},
{"content": "The World Health Organization is headquartered in Geneva.", "label": "ORG: World Health Organization, GPE: Geneva"}
]
###########
st.write("### Named Entity Recognition (NER) Entities")
# Predefined standard entities
ner_entities = [
"PERSON - Names of people, fictional characters, historical figures",
"ORG - Companies, institutions, agencies, teams",
"LOC - Physical locations (mountains, oceans, etc.)",
"GPE - Countries, cities, states, political regions",
"DATE - Calendar dates, years, centuries",
"TIME - Times, durations",
"MONEY - Monetary values with currency"
]
# User can add custom NER types
custom_ner_entities = []
if st.checkbox("Add custom NER entities?"):
num_custom_ner = st.slider("Number of custom NER entities", 1, 10, 1)
for i in range(num_custom_ner):
st.markdown(f"#### Custom Entity {i+1}")
custom_type = st.text_input(f"Entity type {i+1}", key=f"custom_ner_type_{i}")
custom_description = st.text_input(f"Description for {custom_type}", key=f"custom_ner_desc_{i}")
if custom_type and custom_description:
custom_ner_entities.append(f"{custom_type.upper()} - {custom_description}")
# Combine built-in and custom NERs
all_ner_options = ner_entities + custom_ner_entities
selected_entities = st.multiselect(
"Select entities to recognize",
all_ner_options,
default=ner_entities
)
# Extract entity type names (before the dash)
labels = [entity.split(" - ")[0].strip() for entity in selected_entities]
if not labels:
st.warning("Please select at least one entity type.")
labels = ["PERSON"]
##########
# # Extract just the entity type (before the dash)
# labels = [entity.split(" - ")[0] for entity in selected_entities]
# if not labels:
# st.warning("Please select at least one entity type")
# labels = ["PERSON"] # Default if nothing selected
#NNew edit
# elif classification_type == "Multi-Class Classification":
# st.write("### Multi-Class Classification Labels")
# default_labels_by_domain = {
# "News": ["Political", "Sports", "Entertainment", "Technology", "Business"],
# "AG News": ["World", "Sports", "Business", "Sci/Tech"],
# "Tourism": ["Accommodation", "Transportation", "Tourist Attractions",
# "Food & Dining", "Local Experience", "Adventure Activities",
# "Wellness & Spa", "Eco-Friendly Practices", "Family-Friendly",
# "Luxury Tourism"],
# "Restaurant reviews": ["Italian", "French", "American"]
# }
# num_classes = st.slider("Number of classes", 3, 10, 3)
# # Get defaults for selected domain, or empty list
# defaults = default_labels_by_domain.get(domain, [])
# labels = []
# errors = []
# cols = st.columns(3)
# for i in range(num_classes):
# with cols[i % 3]:
# default_value = defaults[i] if i < len(defaults) else ""
# label_input = st.text_input(f"Class {i+1}", default_value)
# normalized_label = label_input.strip().title()
# if not normalized_label:
# errors.append(f"Class {i+1} name is required.")
# else:
# labels.append(normalized_label)
# # Check for duplicates (case-insensitive)
# if len(labels) != len(set(labels)):
# errors.append("Labels names must be unique (case-insensitive, normalized to Title Case).")
# # Show validation results
# if errors:
# for error in errors:
# st.error(error)
# else:
# st.success("All Labels names are valid and unique!")
# labels_valid = not errors # Will be True only if there are no label errors
# else:
# num_classes = st.slider("Number of classes", 3, 23, 3, key="label_num_classes")
# labels = []
# cols = st.columns(3)
# for i in range(num_classes):
# with cols[i % 3]:
# label = st.text_input(f"Class {i+1}", f"Class_{i+1}", key=f"label_class_{i}")
# labels.append(label)
use_few_shot = st.toggle("Use few-shot examples for labeling")
few_shot_examples = []
if use_few_shot:
num_few_shot = st.slider("Number of few-shot examples", 1, 10, 1)
for i in range(num_few_shot):
with st.expander(f"Few-shot Example {i+1}"):
content = st.text_area(f"Content", key=f"label_few_shot_content_{i}")
label = st.selectbox(f"Label", labels, key=f"label_few_shot_label_{i}")
if content and label:
few_shot_examples.append(f"{content}\nLabel: {label}")
num_examples = st.number_input("Number of examples to classify", 1, 100, 1)
examples_to_classify = []
if num_examples <= 20:
for i in range(num_examples):
example = st.text_area(f"Example {i+1}", key=f"example_{i}")
if example:
examples_to_classify.append(example)
else:
examples_text = st.text_area(
"Enter examples (one per line)",
height=300,
help="Enter each example on a new line"
)
if examples_text:
examples_to_classify = [ex.strip() for ex in examples_text.split('\n') if ex.strip()]
if len(examples_to_classify) > num_examples:
examples_to_classify = examples_to_classify[:num_examples]
#New Wedyan
#default_system_role = f"You are a professional {classification_type} expert, your role is to classify the provided text examples for {domain} domain."
# System role customization
default_system_role = (f"You are a highly skilled {classification_type} expert."
f" Your task is to accurately classify the provided text examples within the {domain} domain."
f" Ensure that all classifications are precise, context-aware, and aligned with domain-specific standards and best practices."
)
system_role = st.text_area("Modify System Role (optional)",
value=default_system_role,
key="system_role_input")
st.session_state['system_role'] = system_role if system_role else default_system_role
# Labels initialization
#labels = []
####
user_prompt = st.text_area("User prompt (optional)", key="label_instructions")
few_shot_text = "\n\n".join(few_shot_examples) if few_shot_examples else ""
examples_text = "\n".join([f"{i+1}. {ex}" for i, ex in enumerate(examples_to_classify)])
# Customize prompt template based on classification type
if classification_type == "Named Entity Recognition (NER)":
# label_prompt_template = PromptTemplate(
# input_variables=["system_role", "labels", "few_shot_examples", "examples", "domain", "user_prompt"],
# template=(
# "{system_role}\n"
# #"- You are a professional Named Entity Recognition (NER) expert in {domain} domain. Your role is to identify and extract the following entity types: {labels}.\n"
# "- For each text example provided, identify all entities of the requested types.\n"
# "- Use the following entities: {labels}.\n"
# "- Return each example followed by the entities you found in this format: 'Example text.\n \n Entities:\n [ENTITY_TYPE: entity text\n\n, ENTITY_TYPE: entity text\n\n, ...] or [No entities found]'\n"
# "- If no entities of the requested types are found, indicate 'No entities found' in this text.\n"
# "- Be precise about entity boundaries - don't include unnecessary words.\n"
# "- Do not provide any additional information or explanations.\n"
# "- Additional instructions:\n {user_prompt}\n\n"
# "- Use user few-shot examples as guidance if provided:\n{few_shot_examples}\n\n"
# "- Examples to analyze:\n{examples}\n\n"
# "Output:\n"
# )
# )
#new 22/4/2025
# label_prompt_template = PromptTemplate(
# input_variables=["system_role", "labels", "few_shot_examples", "examples", "domain", "user_prompt"],
# template=(
# "{system_role}\n"
# "- You are performing Named Entity Recognition (NER) in the domain of {domain}.\n"
# "- Use the following entity types: {labels}.\n\n"
# "### Reasoning Steps:\n"
# "1. Read the example carefully.\n"
# "2. For each named entity mentioned, determine its meaning and role in the sentence.\n"
# "3. Think about the **context**: Is it a physical location (LOC)? A geopolitical region (GPE)? A person (PERSON)?\n"
# "4. Based on the definition of each label, assign the most **specific and correct** label.\n\n"
# "For example:\n"
# "- 'Mount Everest' β LOC (it's a mountain)\n"
# "- 'France' β GPE (it's a country)\n"
# "- 'Microsoft' β ORG\n"
# "- 'John Smith' β PERSON\n\n"
# "- Return each example followed by the entities you found in this format:\n"
# "'Example text.'\nEntities: [ENTITY_TYPE: entity text, ENTITY_TYPE: entity text, ...] or [No entities found]\n"
# "- If no entities of the requested types are found, return 'No entities found'.\n"
# "- Be precise about entity boundaries - don't include extra words.\n"
# "- Do not explain or justify your answers.\n\n"
# "Additional instructions:\n{user_prompt}\n\n"
# "Few-shot examples:\n{few_shot_examples}\n\n"
# "Examples to label:\n{examples}\n"
# "Output:\n"
# )
#)
# label_prompt_template = PromptTemplate(
# input_variables=["system_role", "labels", "few_shot_examples", "examples", "domain", "user_prompt"],
# template=(
# "{system_role}\n"
# "- You are an expert at Named Entity Recognition (NER) for domain: {domain}.\n"
# "- Use these entity types: {labels}.\n\n"
# "### Output Format:\n"
# # "Return each example followed by the entities you found in this format: 'Example text.\n Entities:\n [ENTITY_TYPE: entity text\n\"
# "Return each example followed by the entities you found in this format: 'Example text.\n 'Entity types:\n "Then group the entities under each label like this:\n" "
# #"Then Start with this line exactly: 'Entity types\n'\n"
# #"Then group the entities under each label like this:\n"
# "\n PERSON β Angela Merkel, John Smith\n\n"
# "\ ORG β Google, United Nations\n\n"
# "\n DATE β January 1st, 2023\n\n"
# "\n ... and so on.\n\n"
# "If entity {labels} not found, do not write it in your response\n"
# "- Do NOT output them inline after the text.\n"
# "- Do NOT repeat the sentence.\n"
# "- If no entities are found for a type, skip it.\n"
# "- Keep the format consistent.\n\n"
# "User Instructions:\n{user_prompt}\n\n"
# "Few-shot Examples:\n{few_shot_examples}\n\n"
# "Examples to analyze:\n{examples}"
# )
# )
label_prompt_template = PromptTemplate(
input_variables=["system_role", "labels", "few_shot_examples", "examples", "domain", "user_prompt"],
template=(
"{system_role}\n"
"- You are an expert at Named Entity Recognition (NER) for domain: {domain}.\n"
"- Use these entity types: {labels}.\n\n"
"### Output Format:\n"
"Return each example followed by the entities you found in this format:\n"
"'Example text.\n and in new line Entity types:\n"
"Then group the entities under each label like this:\n"
"\nPERSON β [Angela Merkel, John Smith]\n"
"ORG β [Google, United Nations]\n"
"DATE β [January 1st, 2023]\n"
"... and so on.\n\n"
"Each new entities group should be in a new line.\n"
"If entity type {labels} is not found, do not write it in your response.\n"
"- Do NOT output them inline after the text.\n"
"- Do NOT repeat the sentence.\n"
"- If no entities are found for a type, skip it.\n"
"- Keep the format consistent.\n\n"
"User Instructions:\n{user_prompt}\n\n"
"Few-shot Examples:\n{few_shot_examples}\n\n"
"Examples to analyze:\n{examples}"
)
)
#######
else:
label_prompt_template = PromptTemplate(
input_variables=["system_role", "classification_type", "labels", "few_shot_examples", "examples","domain", "user_prompt"],
template=(
#"- Let'\s think step by step:"
"{system_role}\n"
# "- You are a professional {classification_type} expert in {domain} domain. Your role is to classify the following examples using these labels: {labels}.\n"
"- Use the following instructions:\n"
"- Use the following labels: {labels}.\n"
"- In sentiment classification, ensure the output clearly distinguishes between the three categories: Positive, Negative, and Neutral. Each classification should be unambiguous and accurately reflect the sentiment expressed in the text."
"- In binary sentiment classification, restrict the output to either Positive or Negative only. Do not classify or imply Neutral. If the sentiment is ambiguous or mixed, lean toward the dominant tone."
"- Return the classified text followed by the label in this format: 'text. Label: [label]'\n"
"- Do not provide any additional information or explanations\n"
"- User prompt:\n {user_prompt}\n\n"
"- Use user provided examples as guidence in the classification process:\n\n {few_shot_examples}\n"
"- Examples to classify:\n{examples}\n\n"
"- Think step by step then classify the examples"
#"Output:\n"
))
# Check if few_shot_examples is already a formatted string
# Check if few_shot_examples is already a formatted string
if isinstance(few_shot_examples, str):
formatted_few_shot = few_shot_examples
# If it's a list of already formatted strings
elif isinstance(few_shot_examples, list) and all(isinstance(ex, str) for ex in few_shot_examples):
formatted_few_shot = "\n".join(few_shot_examples)
# If it's a list of dictionaries with 'content' and 'label' keys
elif isinstance(few_shot_examples, list) and all(isinstance(ex, dict) and 'content' in ex and 'label' in ex for ex in few_shot_examples):
formatted_few_shot = "\n".join([f"{ex['content']}\nLabel: {ex['label']}" for ex in few_shot_examples])
else:
formatted_few_shot = ""
# #new 22/4/2025
# few_shot_examples = [
# {"content": "Mount Everest is the tallest mountain in the world.", "label": "LOC: Mount Everest"},
# {"content": "The President of the United States visited Paris last summer.", "label": "GPE: United States, GPE: Paris"},
# {"content": "Amazon is expanding its offices in Berlin.", "label": "ORG: Amazon, GPE: Berlin"},
# {"content": "J.K. Rowling wrote the Harry Potter books.", "label": "PERSON: J.K. Rowling"},
# {"content": "Apple was founded in California in 1976.", "label": "ORG: Apple, GPE: California, DATE: 1976"},
# {"content": "The Nile is the longest river in Africa.", "label": "LOC: Nile, GPE: Africa"},
# {"content": "He arrived at 3 PM for the meeting.", "label": "TIME: 3 PM"},
# {"content": "She bought the dress for $200.", "label": "MONEY: $200"},
# {"content": "The event is scheduled for July 4th.", "label": "DATE: July 4th"},
# {"content": "The World Health Organization is headquartered in Geneva.", "label": "ORG: World Health Organization, GPE: Geneva"}
# ]
# ###########
# new 22/4/2025
#formatted_few_shot = "\n".join([f"{ex['content']}\nEntities: [{ex['label']}]" for ex in few_shot_examples])
formatted_few_shot = "\n\n".join([f"{ex['content']}\n\nEntity types\n{ex['label']}" for ex in few_shot_examples])
###########
system_prompt = label_prompt_template.format(
system_role=st.session_state['system_role'],
classification_type=classification_type,
domain=domain,
examples="\n".join(examples_to_classify),
labels=", ".join(labels),
user_prompt=user_prompt,
few_shot_examples=formatted_few_shot
)
# Step 2: Store the system_prompt in st.session_state
st.session_state['system_prompt'] = system_prompt
#::contentReference[oaicite:0]{index=0}
st.write("System Prompt:")
#st.code(system_prompt)
#st.code(st.session_state['system_prompt'])
st.text_area("System Prompt", value=st.session_state['system_prompt'], height=300, max_chars=None, key=None, help=None, disabled=True)
if st.button("π·οΈ Label Data"):
if examples_to_classify:
with st.spinner("Labeling data..."):
#Generate the system prompt based on classification type
if classification_type == "Named Entity Recognition (NER)":
system_prompt = label_prompt_template.format(
system_role=st.session_state['system_role'],
labels=", ".join(labels),
domain = domain,
few_shot_examples=few_shot_text,
examples=examples_text,
user_prompt=user_prompt
#new
#'Use few-shot example?': 'Yes' if use_few_shot else 'No',
)
# if classification_type == "Named Entity Recognition (NER)":
# # Step 1: Split the full response by example
# raw_outputs = [block.strip() for block in response.strip().split("Entity types") if block.strip()]
# inputs = [ex.strip() for ex in examples_to_classify]
# # Step 2: Match inputs with NER output blocks
# labeled_examples = []
# for i, (text, output_block) in enumerate(zip(inputs, raw_outputs)):
# labeled_examples.append({
# 'text': text,
# 'entities': f"Entity types\n{output_block.strip()}",
# 'system_prompt': st.session_state.system_prompt,
# 'system_role': st.session_state.system_role,
# 'task_type': 'Named Entity Recognition (NER)',
# 'Use few-shot example?': 'Yes' if use_few_shot else 'No',
# })
# if classification_type == "Named Entity Recognition (NER)":
# # Step 1: Split the full response by example
# raw_outputs = [block.strip() for block in response.strip().split("Entity types") if block.strip()]
# inputs = [ex.strip() for ex in examples_to_classify]
# # Step 2: Match inputs with NER output blocks
# labeled_examples = []
# for i, (text, output_block) in enumerate(zip(inputs, raw_outputs)):
# labeled_examples.append({
# 'text': text,
# 'entities': f"Entity types\n{output_block.strip()}",
# 'system_prompt': st.session_state.system_prompt,
# 'system_role': st.session_state.system_role,
# 'task_type': 'Named Entity Recognition (NER)',
# 'Use few-shot example?': 'Yes' if use_few_shot else 'No',
# })
# import re
# if classification_type == "Named Entity Recognition (NER)":
# # Use regex to split on "Entity types" while keeping it attached to each block
# blocks = re.split(r"(Entity types)", response.strip())
# # Recombine 'Entity types' with each block after splitting
# raw_outputs = [
# (blocks[i] + blocks[i+1]).strip()
# for i in range(1, len(blocks) - 1, 2)
# ]
# inputs = [ex.strip() for ex in examples_to_classify]
# labeled_examples = []
# for i, (text, output_block) in enumerate(zip(inputs, raw_outputs)):
# labeled_examples.append({
# 'text': text,
# 'entities': output_block,
# 'system_prompt': st.session_state.system_prompt,
# 'system_role': st.session_state.system_role,
# 'task_type': 'Named Entity Recognition (NER)',
# 'Use few-shot example?': 'Yes' if use_few_shot else 'No',
# })
else:
system_prompt = label_prompt_template.format(
classification_type=classification_type,
system_role=st.session_state['system_role'],
domain = domain,
labels=", ".join(labels),
few_shot_examples=few_shot_text,
examples=examples_text,
user_prompt=user_prompt
)
try:
stream = client.chat.completions.create(
model=selected_model,
messages=[{"role": "system", "content": system_prompt}],
temperature=temperature,
stream=True,
max_tokens=20000,
top_p = 0.9,
)
#new 24 March
# Append user message
st.session_state.messages.append({"role": "user", "content": system_prompt})
#################
response = st.write_stream(stream)
st.session_state.messages.append({"role": "assistant", "content": response})
# Display the labeled examples
# # Optional: If you want to add it as a chat-style message log
# preview_str = st.session_state.labeled_preview.to_markdown(index=False)
# st.session_state.messages.append({"role": "assistant", "content": f"Here is a preview of the labeled examples:\n\n{preview_str}"})
# # Stream response and append assistant message
# #14/4/2024
# response = st.write_stream(stream)
# st.session_state.messages.append({"role": "assistant", "content": response})
# Initialize session state variables if they don't exist
if 'system_prompt' not in st.session_state:
st.session_state.system_prompt = system_prompt
if 'response' not in st.session_state:
st.session_state.response = response
if 'generated_examples' not in st.session_state:
st.session_state.generated_examples = []
if 'generated_examples_csv' not in st.session_state:
st.session_state.generated_examples_csv = None
if 'generated_examples_json' not in st.session_state:
st.session_state.generated_examples_json = None
# Save labeled examples to CSV
#new 14/4/2025
#labeled_examples = []
# if classification_type == "Named Entity Recognition (NER)":
# labeled_examples = []
# for line in response.split('\n'):
# if line.strip():
# parts = line.rsplit('Entities:', 1)
# if len(parts) == 2:
# text = parts[0].strip()
# entities = parts[1].strip()
# if text and entities:
# labeled_examples.append({
# 'text': text,
# 'entities': entities,
# 'system_prompt': st.session_state.system_prompt,
# 'system_role': st.session_state.system_role,
# 'task_type': 'Named Entity Recognition (NER)',
# 'Use few-shot example?': 'Yes' if use_few_shot else 'No',
# })
#new 22/4/2025
###25/4/2025
labeled_examples = []
if classification_type == "Named Entity Recognition (NER)":
labeled_examples = [{
'ner_output': response.strip(),
'system_prompt': st.session_state.system_prompt,
'system_role': st.session_state.system_role,
'task_type': 'Named Entity Recognition (NER)',
'Use few-shot example?': 'Yes' if use_few_shot else 'No',
}]
######
else:
labeled_examples = []
for line in response.split('\n'):
if line.strip():
parts = line.rsplit('Label:', 1)
if len(parts) == 2:
text = parts[0].strip()
label = parts[1].strip()
if text and label:
labeled_examples.append({
'text': text,
'label': label,
'system_prompt': st.session_state.system_prompt,
'system_role': st.session_state.system_role,
'task_type': 'Data Labeling',
'Use few-shot example?': 'Yes' if use_few_shot else 'No',
})
# ###NEW 25/4/2025
# labeled_examples = []
# if classification_type == "Named Entity Recognition (NER)":
# # Parse the NER response
# text_and_entities = []
# current_text = []
# current_entities = []
# lines = response.strip().split('\n')
# entity_section = False
# for line in lines:
# if line.strip() == "Entity types:" or line.strip() == "Entities:":
# entity_section = True
# continue
# if not entity_section:
# current_text.append(line.strip())
# else:
# if line.strip():
# current_entities.append(line.strip())
# # Create the example dictionary
# labeled_examples = [{
# 'text': ' '.join(current_text).strip(),
# 'ner_output': '\n'.join(current_entities).strip(),
# 'system_prompt': st.session_state.system_prompt,
# 'system_role': st.session_state.system_role,
# 'task_type': 'Named Entity Recognition (NER)',
# 'Use few-shot example?': 'Yes' if use_few_shot else 'No',
# }]
# else:
# # Rest of your existing code for data labeling
# labeled_examples = []
# for line in response.split('\n'):
# if line.strip():
# parts = line.rsplit('Label:', 1)
# if len(parts) == 2:
# text = parts[0].strip()
# label = parts[1].strip()
# if text and label:
# labeled_examples.append({
# 'text': text,
# 'label': label,
# 'system_prompt': st.session_state.system_prompt,
# 'system_role': st.session_state.system_role,
# 'task_type': 'Data Labeling',
# 'Use few-shot example?': 'Yes' if use_few_shot else 'No',
# })
#25/4/2025 newset
labeled_examples = []
if classification_type == "Named Entity Recognition (NER)":
# Parse the NER response
text_and_entities = []
current_text = []
current_entities = []
lines = response.strip().split('\n')
entity_section = False
for line in lines:
if line.strip() == "Entity types:" or line.strip() == "Entities:":
entity_section = True
continue
if not entity_section:
current_text.append(line.strip())
else:
if line.strip():
current_entities.append(line.strip())
# Create the example dictionary with all fields including few-shot
labeled_examples = [{
'text': ' '.join(current_text).strip(),
'ner_output': '\n'.join(current_entities).strip(),
'system_prompt': st.session_state.system_prompt,
'system_role': st.session_state.system_role,
'task_type': 'Named Entity Recognition (NER)',
'Use few-shot example?': 'Yes' if use_few_shot else 'No', # Few-shot flag
'selected_entities': ', '.join(selected_entities),
'custom_entities': ', '.join(custom_ner_entities) if custom_ner_entities else 'None'
}]
else:
# Rest of your existing code for data labeling
labeled_examples = []
for line in response.split('\n'):
if line.strip():
parts = line.rsplit('Label:', 1)
if len(parts) == 2:
text = parts[0].strip()
label = parts[1].strip()
if text and label:
labeled_examples.append({
'text': text,
'label': label,
'system_prompt': st.session_state.system_prompt,
'system_role': st.session_state.system_role,
'task_type': 'Data Labeling',
'Use few-shot example?': 'Yes' if use_few_shot else 'No',
})
#remove 25/4/2025
# # Save and provide download options
# if labeled_examples:
# # Update session state
# st.session_state.labeled_examples = labeled_examples
# # Convert to CSV and JSON
# df = pd.DataFrame(labeled_examples)
# #new 22/4/2025
# # CSV
# st.session_state.labeled_examples_csv = df.to_csv(index=False).encode('utf-8')
# # JSON
# st.session_state.labeled_examples_json = json.dumps({
# "metadata": {
# "domain": domain,
# "labels": labels,
# "used_few_shot": use_few_shot,
# "task_type": "Named Entity Recognition (NER)",
# "timestamp": datetime.now().isoformat()
# },
# "examples": labeled_examples
# }, indent=2).encode('utf-8')
# Save and provide download options
if labeled_examples:
# Update session state
st.session_state.labeled_examples = labeled_examples
# Convert to CSV and JSON
df = pd.DataFrame(labeled_examples)
# CSV
st.session_state.labeled_examples_csv = df.to_csv(index=False).encode('utf-8')
# JSON with conditional metadata based on classification type
if classification_type == "Named Entity Recognition (NER)":
metadata = {
"domain": domain,
"selected_entities": selected_entities,
"custom_entities": custom_ner_entities,
"used_few_shot": use_few_shot,
"task_type": "Named Entity Recognition (NER)",
"timestamp": datetime.now().isoformat()
}
else:
metadata = {
"domain": domain,
"labels": labels,
"used_few_shot": use_few_shot,
"task_type": "Data Labeling",
"timestamp": datetime.now().isoformat()
}
st.session_state.labeled_examples_json = json.dumps({
"metadata": metadata,
"examples": labeled_examples
}, indent=2).encode('utf-8')
############
# CSV
# st.session_state.labeled_examples_csv = df.to_csv(index=False).encode('utf-8')
# # JSON
# st.session_state.labeled_examples_json = json.dumps({
# "metadata": {
# "domain": domain,
# "labels": labels,
# "used_few_shot": use_few_shot,
# "task_type": "Named Entity Recognition (NER)",
# "timestamp": datetime.now().isoformat()
# },
# "examples": labeled_examples
# }, indent=2).encode('utf-8')
########
st.session_state.labeled_examples_csv = df.to_csv(index=False).encode('utf-8')
st.session_state.labeled_examples_json = json.dumps(labeled_examples, indent=2).encode('utf-8')
# Download buttons
st.download_button(
"π₯ Download Labeled Examples (CSV)",
st.session_state.labeled_examples_csv,
"labeled_examples.csv",
"text/csv",
key='download-labeled-csv'
)
st.markdown("""
<div style='text-align: left; margin:15px 0; font-weight: 600; color: #666;'>. . . . . . or</div>
""", unsafe_allow_html=True)
st.download_button(
"π₯ Download Labeled Examples (JSON)",
st.session_state.labeled_examples_json,
"labeled_examples.json",
"application/json",
key='download-labeled-json'
)
# Display the labeled examples
st.markdown("##### π Labeled Examples Preview")
st.dataframe(df, use_container_width=True)
# Display section
#st.markdown("### π Labeled Examples Preview")
#st.dataframe(st.session_state.labeled_preview, use_container_width=True)
# if labeled_examples:
# df = pd.DataFrame(labeled_examples)
# csv = df.to_csv(index=False).encode('utf-8')
# st.download_button(
# "π₯ Download Labeled Examples",
# csv,
# "labeled_examples.csv",
# "text/csv",
# key='download-labeled-csv'
# )
# # Add space and center the "or"
# st.markdown("""
# <div style='text-align: left; margin:15px 0; font-weight: 600; color: #666;'>. . . . . . or</div>
# """, unsafe_allow_html=True)
# if labeled_examples:
# df = pd.DataFrame(labeled_examples)
# csv = df.to_csv(index=False).encode('utf-8')
# st.download_button(
# "π₯ Download Labeled Examples",
# csv,
# "labeled_examples.json",
# "text/json",
# key='download-labeled-JSON'
# )
# Add follow-up interaction options
#st.markdown("---")
#follow_up = st.radio(
#"What would you like to do next?",
#["Label more data", "Data Generation"],
# key="labeling_follow_up"
# )
if st.button("Continue"):
if follow_up == "Label more data":
st.session_state.examples_to_classify = []
st.experimental_rerun()
elif follow_up == "Data Generation":
st.session_state.task_choice = "Data Labeling"
st.experimental_rerun()
except Exception as e:
st.error("An error occurred during labeling.")
st.error(f"Details: {e}")
else:
st.warning("Please enter at least one example to classify.")
#st.session_state.messages.append({"role": "assistant", "content": response})
# Footer
st.markdown("---")
st.markdown(
"""
<div style='text-align: center'>
<p>Made with β€οΈ by Wedyan AlSakran 2025</p>
</div>
""",
unsafe_allow_html=True
) |