File size: 8,641 Bytes
255f60b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
### اول كود للابيلنق اشتغل بس مافرق بين ريكوند و نت ريكومند
import numpy as np
import streamlit as st
from openai import OpenAI
import os
from dotenv import load_dotenv
import random

# Load environment variables
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('GP2')  # Replace with your Huggingface token
)

# Initialize session state variables if they are not already defined
if "labels" not in st.session_state:
    st.session_state.labels = []
if "few_shot_examples" not in st.session_state:
    st.session_state.few_shot_examples = []
if "examples_to_classify" not in st.session_state:
    st.session_state.examples_to_classify = []
if "messages" not in st.session_state:
    st.session_state.messages = []

# Sidebar for model selection and temperature setting
selected_model = st.sidebar.selectbox("Select Model", ["Meta-Llama-3-8B"], key="model_select")
temp_values = st.sidebar.slider('Select a temperature value', 0.0, 1.0, 0.5, key="temp_slider")

# Reset conversation button
st.sidebar.button('Reset Chat', on_click=lambda: (st.session_state.update(conversation=[], messages=[])), key="reset_button")

# Main task selection: Data Generation or Data Labeling
task_choice = st.selectbox("Choose Task", ["Data Generation", "Data Labeling"], key="task_choice_select")

# Data Generation Section
if task_choice == "Data Generation":
    classification_type = st.selectbox(
        "Choose Classification Type",
        ["Sentiment Analysis", "Binary Classification", "Multi-Class Classification"],
        key="classification_type_select"
    )

    # Define labels based on classification type
    if classification_type == "Sentiment Analysis":
        st.session_state.labels = ["Positive", "Negative", "Neutral"]
        st.write("Sentiment Analysis: Positive, Negative, Neutral")
    elif classification_type == "Binary Classification":
        label_1 = st.text_input("Enter first class", key="binary_class_1")
        label_2 = st.text_input("Enter second class", key="binary_class_2")
        st.session_state.labels = [label_1, label_2]
    elif classification_type == "Multi-Class Classification":
        num_classes = st.slider("How many classes?", 3, 10, 3, key="num_classes_slider")
        st.session_state.labels = [st.text_input(f"Class {i+1}", key=f"class_input_{i+1}") for i in range(num_classes)]

    # Domain selection
    domain = st.selectbox("Choose Domain", ["Restaurant reviews", "E-commerce reviews", "Custom"], key="domain_select")
    if domain == "Custom":
        domain = st.text_input("Specify custom domain", key="custom_domain_input")

    # Word count selection
    min_words = st.number_input("Minimum words per example", min_value=10, max_value=90, value=10, key="min_words_input")
    max_words = st.number_input("Maximum words per example", min_value=10, max_value=90, value=90, key="max_words_input")

    # Few-shot examples option
    few_shot = st.radio("Do you want to use few-shot examples?", ["Yes", "No"], key="few_shot_radio")
    if few_shot == "Yes":
        num_examples = st.slider("How many few-shot examples?", 1, 5, 1, key="num_examples_slider")
        st.session_state.few_shot_examples = [
            {
                "content": st.text_area(f"Example {i+1} Text", key=f"example_text_{i+1}"),
                "label": st.selectbox(f"Label for Example {i+1}", st.session_state.labels, key=f"label_select_{i+1}")
            }
            for i in range(num_examples)
        ]
    else:
        st.session_state.few_shot_examples = []

    # Number of examples to generate
    num_to_generate = st.number_input("How many examples to generate?", min_value=1, max_value=100, value=10, key="num_to_generate_input")

    # User prompt text field
    user_prompt = st.text_area("Enter your prompt to guide example generation", "", key="user_prompt_text_area")

    # 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 st.session_state.few_shot_examples:
        system_prompt += "Use the following few-shot examples as a reference:\n"
        for example in st.session_state.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(st.session_state.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", key="generate_examples_button"):
        # Generate examples by concatenating all inputs and sending it to the model
        with st.spinner("Generating..."):
            st.session_state.messages.append({"role": "system", "content": system_prompt})

            try:
                stream = client.chat.completions.create(
                    model=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 = ""
                for chunk in stream:
                    response += chunk['choices'][0]['delta'].get('content', '')
                st.write(response)
            except Exception as e:
                st.error(f"Error during generation: {e}")

            st.session_state.messages.append({"role": "assistant", "content": response})

# Data Labeling Section
else:
    # Classification Type and Labels Setup
    classification_type = st.selectbox("Choose Classification Type", ["Sentiment Analysis", "Binary Classification", "Multi-Class Classification"], key="classification_type_labeling")
    
    if classification_type == "Sentiment Analysis":
        st.session_state.labels = ["Positive", "Negative", "Neutral"]
        st.write("Sentiment Analysis labels: Positive, Negative, Neutral")
    elif classification_type == "Binary Classification":
        label_1 = st.text_input("Enter first class", key="binary_class_1_labeling")
        label_2 = st.text_input("Enter second class", key="binary_class_2_labeling")
        st.session_state.labels = [label_1, label_2]
    elif classification_type == "Multi-Class Classification":
        num_classes = st.slider("How many classes?", 3, 10, 3, key="num_classes_labeling")
        st.session_state.labels = [st.text_input(f"Class {i+1}", key=f"class_input_labeling_{i+1}") for i in range(num_classes)]

    # Few-shot examples for labeling
    use_few_shot = st.radio("Do you want to use few-shot examples?", ["Yes", "No"], key="use_few_shot_labeling")
    if use_few_shot == "Yes":
        num_examples = st.slider("How many few-shot examples?", 1, 5, 1, key="few_shot_num_labeling")
        st.session_state.few_shot_examples = [
            {
                "content": st.text_area(f"Example {i+1} Text", key=f"example_text_labeling_{i+1}"),
                "label": st.selectbox(f"Label for Example {i+1}", st.session_state.labels, key=f"label_select_labeling_{i+1}")
            }
            for i in range(num_examples)
        ]
    else:
        st.session_state.few_shot_examples = []

    # Input Examples for Classification
    num_to_classify = st.number_input("How many examples do you want to classify?", min_value=1, max_value=100, value=5, key="num_to_classify_input")
    st.session_state.examples_to_classify = [st.text_area(f"Example {i+1} Text", key=f"example_classify_text_{i+1}") for i in range(num_to_classify)]

    # Placeholder for classification function (can be replaced with actual API call)
    def classify_examples(examples, labels):
        classified_results = [{"example": ex, "label": random.choice(labels)} for ex in examples]
        return classified_results

    # Classification results display
    if st.button("Classify Examples", key="classify_button"):
        results = classify_examples(st.session_state.examples_to_classify, st.session_state.labels)
        st.write("Classification Results:")
        for result in results:
            st.write(f"Example: {result['example']}\nLabel: {result['label']}\n")
شحح