Spaces:
Sleeping
Sleeping
File size: 8,354 Bytes
59f85b8 3d079ce 59f85b8 |
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 |
import numpy as np
import streamlit as st
from openai import OpenAI
import os
from dotenv import load_dotenv
import random
os.environ["BROWSER_GATHERUSAGESTATS"] = "false"
load_dotenv()
# Initialize the client
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1",
api_key=os.environ.get('TOKEN2') # Add your Huggingface token here
)
# Supported models
model_links = {
"Meta-Llama-3-8B": "meta-llama/Meta-Llama-3-8B-Instruct"
}
# Reset conversation
def reset_conversation():
st.session_state.conversation = []
st.session_state.messages = []
# Define the available models
models = [key for key in model_links.keys()]
# Sidebar for model selection
selected_model = st.sidebar.selectbox("Select Model", models)
temp_values = st.sidebar.slider('Select a temperature value', 0.0, 1.0, 0.5)
st.sidebar.button('Reset Chat', on_click=reset_conversation)
st.sidebar.write(f"You're now chatting with **{selected_model}**")
st.sidebar.markdown("*Generated content may be inaccurate or false.*")
# Chat initialization
if "messages" not in st.session_state:
st.session_state.messages = []
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Main logic to choose between data generation and data labeling
task_choice = st.selectbox("Choose Task", ["Data Generation", "Data Labeling"])
if task_choice == "Data Generation":
classification_type = st.selectbox(
"Choose Classification Type",
["Sentiment Analysis", "Binary Classification", "Multi-Class Classification"]
)
if classification_type == "Sentiment Analysis":
st.write("Sentiment Analysis: Positive, Negative, Neutral")
labels = ["Positive", "Negative", "Neutral"]
elif classification_type == "Binary Classification":
label_1 = st.text_input("Enter first class")
label_2 = st.text_input("Enter second class")
labels = [label_1, label_2]
elif classification_type == "Multi-Class Classification":
num_classes = st.slider("How many classes?", 3, 10, 3)
labels = [st.text_input(f"Class {i+1}") for i in range(num_classes)]
domain = st.selectbox("Choose Domain", ["Restaurant reviews", "E-commerce reviews", "Custom"])
if domain == "Custom":
domain = st.text_input("Specify custom domain")
min_words = st.number_input("Minimum words per example", min_value=10, max_value=90, value=10)
max_words = st.number_input("Maximum words per example", min_value=10, max_value=90, value=90)
few_shot = st.radio("Do you want to use few-shot examples?", ["Yes", "No"])
if few_shot == "Yes":
num_examples = st.slider("How many few-shot examples?", 1, 5, 1)
few_shot_examples = [
{"content": st.text_area(f"Example {i+1}"), "label": st.selectbox(f"Label for example {i+1}", labels)}
for i in range(num_examples)
]
else:
few_shot_examples = []
num_to_generate = st.number_input("How many examples to generate?", min_value=1, max_value=100, value=10)
user_prompt = st.text_area("Enter your prompt to guide example generation", "")
# System prompt generation
system_prompt = f"You are a professional {classification_type.lower()} expert. Your role is to generate data for {domain}.\n\n"
if few_shot_examples:
system_prompt += "Use the following few-shot examples as a reference:\n"
for example in few_shot_examples:
system_prompt += f"Example: {example['content']} \n Label: {example['label']}\n"
system_prompt += f"Generate {num_to_generate} unique examples with diverse phrasing.\n"
system_prompt += f"Each example should have between {min_words} and {max_words} words.\n"
system_prompt += f"Use the labels specified: {', '.join(labels)}.\n"
if user_prompt:
system_prompt += f"Additional instructions: {user_prompt}\n"
st.write("System Prompt:")
st.code(system_prompt)
if st.button("Generate Examples"):
with st.spinner("Generating..."):
st.session_state.messages.append({"role": "system", "content": system_prompt})
try:
stream = client.chat.completions.create(
model=model_links[selected_model],
messages=[
{"role": m["role"], "content": m["content"]}
for m in st.session_state.messages
],
temperature=temp_values,
stream=True,
max_tokens=3000,
)
response = st.write_stream(stream)
except Exception as e:
st.error("An error occurred during generation.")
st.error(f"Error details: {str(e)}")
st.session_state.messages.append({"role": "assistant", "content": response})
else: # Data Labeling Process
labeling_classification_type = st.selectbox(
"Choose Classification Type for Labeling",
["Sentiment Analysis", "Binary Classification", "Multi-Class Classification"]
)
if labeling_classification_type == "Sentiment Analysis":
st.write("Sentiment Analysis: Positive, Negative, Neutral")
labeling_labels = ["Positive", "Negative", "Neutral"]
elif labeling_classification_type == "Binary Classification":
labeling_label_1 = st.text_input("Enter first class for labeling")
labeling_label_2 = st.text_input("Enter second class for labeling")
labeling_labels = [labeling_label_1, labeling_label_2]
elif labeling_classification_type == "Multi-Class Classification":
labeling_num_classes = st.slider("How many classes for labeling?", 3, 10, 3)
labeling_labels = [st.text_input(f"Labeling Class {i+1}") for i in range(labeling_num_classes)]
labeling_few_shot = st.radio("Do you want to add few-shot examples for labeling?", ["Yes", "No"])
if labeling_few_shot == "Yes":
labeling_num_examples = st.slider("How many few-shot examples for labeling?", 1, 5, 1)
labeling_few_shot_examples = [
{"content": st.text_area(f"Labeling Example {i+1}"),
"label": st.selectbox(f"Label for labeling example {i+1}", labeling_labels)}
for i in range(labeling_num_examples)
]
else:
labeling_few_shot_examples = []
text_to_classify = st.text_area("Enter text to classify")
if st.button("Classify Text"):
if text_to_classify:
labeling_system_prompt = (
f"You are a professional {labeling_classification_type.lower()} expert. "
f"Classify the following text using these labels: {', '.join(labeling_labels)}.\n\n"
)
if labeling_few_shot_examples:
labeling_system_prompt += "Here are some example classifications:\n"
for example in labeling_few_shot_examples:
labeling_system_prompt += f"Review: {example['content']}\nLabel: {example['label']}\n\n"
labeling_system_prompt += (
f"Text to classify: {text_to_classify}\n"
f"Provide only the classification result in this format:\n"
f"'Review: [text provided] Label: [appropriate label]'\n"
)
with st.spinner("Classifying..."):
st.session_state.messages.append({"role": "system", "content": labeling_system_prompt})
try:
stream = client.chat.completions.create(
model=model_links[selected_model],
messages=[
{"role": m["role"], "content": m["content"]}
for m in st.session_state.messages
],
temperature=temp_values,
stream=True,
max_tokens=1000,
)
response = st.write_stream(stream)
except Exception as e:
st.error("An error occurred during classification.")
st.error(f"Error details: {str(e)}")
st.session_state.messages.append({"role": "assistant", "content": response})
else:
st.warning("Please enter text to classify.")
|