Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,131 +7,142 @@ import numpy as np
|
|
| 7 |
import streamlit as st
|
| 8 |
from openai import OpenAI
|
| 9 |
import os
|
|
|
|
| 10 |
from dotenv import load_dotenv
|
| 11 |
-
#streamlit
|
| 12 |
|
| 13 |
load_dotenv()
|
| 14 |
|
| 15 |
# Initialize the client
|
| 16 |
client = OpenAI(
|
| 17 |
base_url="https://api-inference.huggingface.co/v1",
|
| 18 |
-
api_key=os.environ.get('HUGGINGFACEHUB_API_TOKEN') #
|
| 19 |
)
|
| 20 |
|
| 21 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def reset_conversation():
|
| 23 |
st.session_state.conversation = []
|
| 24 |
st.session_state.messages = []
|
| 25 |
return None
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
st.session_state.messages = []
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
st.sidebar.
|
| 36 |
-
task = st.sidebar.radio("Do you want to generate data or label data?", ("Data Generation", "Data Labeling"))
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
st.sidebar.write("Choose Classification Type:")
|
| 41 |
-
classification_type = st.sidebar.radio("Select a classification type:", classification_types)
|
| 42 |
-
|
| 43 |
-
# Handle Sentiment Analysis
|
| 44 |
-
if classification_type == "Sentiment Analysis":
|
| 45 |
-
st.sidebar.write("Classes: Positive, Negative, Neutral (fixed)")
|
| 46 |
-
class_labels = ["Positive", "Negative", "Neutral"]
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
class_2 = st.sidebar.text_input("Enter Class 2:")
|
| 52 |
-
class_labels = [class_1, class_2]
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
for i in range(1, 11): # Allow up to 10 classes
|
| 58 |
-
label = st.sidebar.text_input(f"Enter Class {i} (leave blank to stop):")
|
| 59 |
-
if label:
|
| 60 |
-
class_labels.append(label)
|
| 61 |
-
else:
|
| 62 |
-
break
|
| 63 |
-
|
| 64 |
-
# Domain selection
|
| 65 |
-
st.sidebar.write("Specify the Domain:")
|
| 66 |
-
domain = st.sidebar.radio("Choose a domain:", ("Restaurant Reviews", "E-commerce Reviews", "Custom"))
|
| 67 |
-
if domain == "Custom":
|
| 68 |
-
domain = st.sidebar.text_input("Enter Custom Domain:")
|
| 69 |
-
|
| 70 |
-
# Specify example length
|
| 71 |
-
st.sidebar.write("Specify the Length of Examples:")
|
| 72 |
-
min_words = st.sidebar.number_input("Minimum word count (10 to 90):", 10, 90, 10)
|
| 73 |
-
max_words = st.sidebar.number_input("Maximum word count (10 to 90):", min_words, 90, 50)
|
| 74 |
-
|
| 75 |
-
# Few-shot examples option
|
| 76 |
-
use_few_shot = st.sidebar.radio("Do you want to use few-shot examples?", ("Yes", "No"))
|
| 77 |
-
few_shot_examples = []
|
| 78 |
-
if use_few_shot == "Yes":
|
| 79 |
-
num_examples = st.sidebar.number_input("How many few-shot examples? (1 to 5)", 1, 5, 1)
|
| 80 |
-
for i in range(num_examples):
|
| 81 |
-
example_text = st.text_area(f"Enter example {i+1}:")
|
| 82 |
-
example_label = st.selectbox(f"Select the label for example {i+1}:", class_labels)
|
| 83 |
-
few_shot_examples.append({"text": example_text, "label": example_label})
|
| 84 |
-
|
| 85 |
-
# Generate the system prompt based on classification type
|
| 86 |
-
if classification_type == "Sentiment Analysis":
|
| 87 |
-
system_prompt = f"You are a propositional sentiment analysis expert. Your role is to generate sentiment analysis reviews based on the data entered and few-shot examples provided, if any, for the domain '{domain}'."
|
| 88 |
-
elif classification_type == "Binary Classification":
|
| 89 |
-
system_prompt = f"You are an expert in binary classification. Your task is to label examples for the domain '{domain}' with either '{class_1}' or '{class_2}', based on the data provided."
|
| 90 |
-
else: # Multi-Class Classification
|
| 91 |
-
system_prompt = f"You are an expert in multi-class classification. Your role is to label examples for the domain '{domain}' using the provided class labels."
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
st.sidebar.write("Think step by step to ensure accuracy in classification.")
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
try:
|
| 113 |
-
# Stream the response from the model
|
| 114 |
stream = client.chat.completions.create(
|
| 115 |
-
model=
|
| 116 |
messages=[
|
| 117 |
{"role": m["role"], "content": m["content"]}
|
| 118 |
for m in st.session_state.messages
|
| 119 |
],
|
| 120 |
-
temperature=
|
| 121 |
stream=True,
|
| 122 |
max_tokens=3000,
|
| 123 |
)
|
| 124 |
-
|
| 125 |
response = st.write_stream(stream)
|
| 126 |
-
|
| 127 |
except Exception as e:
|
| 128 |
-
response = "
|
| 129 |
-
|
|
|
|
|
|
|
| 130 |
|
| 131 |
-
|
| 132 |
|
| 133 |
-
# If the user selects Data Generation
|
| 134 |
else:
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
|
|
|
|
| 7 |
import streamlit as st
|
| 8 |
from openai import OpenAI
|
| 9 |
import os
|
| 10 |
+
import sys
|
| 11 |
from dotenv import load_dotenv
|
|
|
|
| 12 |
|
| 13 |
load_dotenv()
|
| 14 |
|
| 15 |
# Initialize the client
|
| 16 |
client = OpenAI(
|
| 17 |
base_url="https://api-inference.huggingface.co/v1",
|
| 18 |
+
api_key=os.environ.get('HUGGINGFACEHUB_API_TOKEN') # Add your Huggingface token here
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# Supported models
|
| 22 |
+
model_links = {
|
| 23 |
+
"Meta-Llama-3-8B": "meta-llama/Meta-Llama-3-8B-Instruct"
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
# Random dog images for error messages
|
| 27 |
+
random_dog = [
|
| 28 |
+
"0f476473-2d8b-415e-b944-483768418a95.jpg",
|
| 29 |
+
"1bd75c81-f1d7-4e55-9310-a27595fa8762.jpg",
|
| 30 |
+
"526590d2-8817-4ff0-8c62-fdcba5306d02.jpg",
|
| 31 |
+
"1326984c-39b0-492c-a773-f120d747a7e2.jpg"
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
# Reset conversation
|
| 35 |
def reset_conversation():
|
| 36 |
st.session_state.conversation = []
|
| 37 |
st.session_state.messages = []
|
| 38 |
return None
|
| 39 |
|
| 40 |
+
# Define the available models
|
| 41 |
+
models = [key for key in model_links.keys()]
|
|
|
|
| 42 |
|
| 43 |
+
# Sidebar for model selection
|
| 44 |
+
selected_model = st.sidebar.selectbox("Select Model", models)
|
| 45 |
|
| 46 |
+
# Temperature slider
|
| 47 |
+
temp_values = st.sidebar.slider('Select a temperature value', 0.0, 1.0, 0.5)
|
|
|
|
| 48 |
|
| 49 |
+
# Reset button
|
| 50 |
+
st.sidebar.button('Reset Chat', on_click=reset_conversation)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
# Model description
|
| 53 |
+
st.sidebar.write(f"You're now chatting with **{selected_model}**")
|
| 54 |
+
st.sidebar.markdown("*Generated content may be inaccurate or false.*")
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
# Chat initialization
|
| 57 |
+
if "messages" not in st.session_state:
|
| 58 |
+
st.session_state.messages = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
# Display chat messages
|
| 61 |
+
for message in st.session_state.messages:
|
| 62 |
+
with st.chat_message(message["role"]):
|
| 63 |
+
st.markdown(message["content"])
|
| 64 |
|
| 65 |
+
# Main logic to choose between data generation and data labeling
|
| 66 |
+
task_choice = st.selectbox("Choose Task", ["Data Generation", "Data Labeling"])
|
|
|
|
| 67 |
|
| 68 |
+
if task_choice == "Data Generation":
|
| 69 |
+
classification_type = st.selectbox(
|
| 70 |
+
"Choose Classification Type",
|
| 71 |
+
["Sentiment Analysis", "Binary Classification", "Multi-Class Classification"]
|
| 72 |
+
)
|
| 73 |
|
| 74 |
+
if classification_type == "Sentiment Analysis":
|
| 75 |
+
st.write("Sentiment Analysis: Positive, Negative, Neutral")
|
| 76 |
+
labels = ["Positive", "Negative", "Neutral"]
|
| 77 |
+
elif classification_type == "Binary Classification":
|
| 78 |
+
label_1 = st.text_input("Enter first class")
|
| 79 |
+
label_2 = st.text_input("Enter second class")
|
| 80 |
+
labels = [label_1, label_2]
|
| 81 |
+
elif classification_type == "Multi-Class Classification":
|
| 82 |
+
num_classes = st.slider("How many classes?", 3, 10, 3)
|
| 83 |
+
labels = [st.text_input(f"Class {i+1}") for i in range(num_classes)]
|
| 84 |
|
| 85 |
+
domain = st.selectbox("Choose Domain", ["Restaurant reviews", "E-commerce reviews", "Custom"])
|
| 86 |
+
if domain == "Custom":
|
| 87 |
+
domain = st.text_input("Specify custom domain")
|
| 88 |
+
|
| 89 |
+
min_words = st.number_input("Minimum words per example", min_value=10, max_value=90, value=10)
|
| 90 |
+
max_words = st.number_input("Maximum words per example", min_value=10, max_value=90, value=90)
|
| 91 |
+
|
| 92 |
+
few_shot = st.radio("Do you want to use few-shot examples?", ["Yes", "No"])
|
| 93 |
+
if few_shot == "Yes":
|
| 94 |
+
num_examples = st.slider("How many few-shot examples?", 1, 5, 1)
|
| 95 |
+
few_shot_examples = [
|
| 96 |
+
{"content": st.text_area(f"Example {i+1}"), "label": st.selectbox(f"Label for example {i+1}", labels)}
|
| 97 |
+
for i in range(num_examples)
|
| 98 |
+
]
|
| 99 |
+
else:
|
| 100 |
+
few_shot_examples = []
|
| 101 |
+
|
| 102 |
+
# Ask the user how many examples they need
|
| 103 |
+
num_to_generate = st.number_input("How many examples to generate?", min_value=1, max_value=50, value=10)
|
| 104 |
+
|
| 105 |
+
# System prompt generation
|
| 106 |
+
system_prompt = f"You are a professional {classification_type.lower()} expert. Your role is to generate data for {domain}.\n\n"
|
| 107 |
+
if few_shot_examples:
|
| 108 |
+
system_prompt += "Use the following few-shot examples as a reference:\n"
|
| 109 |
+
for example in few_shot_examples:
|
| 110 |
+
system_prompt += f"Example: {example['content']}, Label: {example['label']}\n"
|
| 111 |
+
system_prompt += f"Each example should have between {min_words} and {max_words} words.\n"
|
| 112 |
+
system_prompt += "Think step by step while generating the examples."
|
| 113 |
+
|
| 114 |
+
st.write("System Prompt:")
|
| 115 |
+
st.code(system_prompt)
|
| 116 |
+
|
| 117 |
+
if st.button("Generate Examples"):
|
| 118 |
+
# Generate examples by concatenating all inputs and sending it to the model
|
| 119 |
+
with st.spinner("Generating..."):
|
| 120 |
+
st.session_state.messages.append({"role": "system", "content": system_prompt})
|
| 121 |
|
| 122 |
try:
|
|
|
|
| 123 |
stream = client.chat.completions.create(
|
| 124 |
+
model=model_links[selected_model],
|
| 125 |
messages=[
|
| 126 |
{"role": m["role"], "content": m["content"]}
|
| 127 |
for m in st.session_state.messages
|
| 128 |
],
|
| 129 |
+
temperature=temp_values,
|
| 130 |
stream=True,
|
| 131 |
max_tokens=3000,
|
| 132 |
)
|
|
|
|
| 133 |
response = st.write_stream(stream)
|
|
|
|
| 134 |
except Exception as e:
|
| 135 |
+
response = "Error during generation."
|
| 136 |
+
random_dog_pick = 'https://random.dog/' + random_dog[np.random.randint(len(random_dog))]
|
| 137 |
+
st.image(random_dog_pick)
|
| 138 |
+
st.write(e)
|
| 139 |
|
| 140 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 141 |
|
|
|
|
| 142 |
else:
|
| 143 |
+
# Data labeling workflow (for future implementation based on classification)
|
| 144 |
+
st.write("Data Labeling functionality will go here.")
|
| 145 |
+
|
| 146 |
+
|
| 147 |
|
| 148 |
|