Delete app4.py
Browse files
app4.py
DELETED
@@ -1,173 +0,0 @@
|
|
1 |
-
### اول كود للابيلنق اشتغل بس مافرق بين ريكوند و نت ريكومند
|
2 |
-
import numpy as np
|
3 |
-
import streamlit as st
|
4 |
-
from openai import OpenAI
|
5 |
-
import os
|
6 |
-
from dotenv import load_dotenv
|
7 |
-
import random
|
8 |
-
|
9 |
-
# Load environment variables
|
10 |
-
os.environ["BROWSER_GATHERUSAGESTATS"] = "false"
|
11 |
-
load_dotenv()
|
12 |
-
|
13 |
-
# Initialize the client
|
14 |
-
client = OpenAI(
|
15 |
-
base_url="https://api-inference.huggingface.co/v1",
|
16 |
-
api_key=os.environ.get('GP2') # Replace with your Huggingface token
|
17 |
-
)
|
18 |
-
|
19 |
-
# Initialize session state variables if they are not already defined
|
20 |
-
if "labels" not in st.session_state:
|
21 |
-
st.session_state.labels = []
|
22 |
-
if "few_shot_examples" not in st.session_state:
|
23 |
-
st.session_state.few_shot_examples = []
|
24 |
-
if "examples_to_classify" not in st.session_state:
|
25 |
-
st.session_state.examples_to_classify = []
|
26 |
-
if "messages" not in st.session_state:
|
27 |
-
st.session_state.messages = []
|
28 |
-
|
29 |
-
# Sidebar for model selection and temperature setting
|
30 |
-
selected_model = st.sidebar.selectbox("Select Model", ["Meta-Llama-3-8B"], key="model_select")
|
31 |
-
temp_values = st.sidebar.slider('Select a temperature value', 0.0, 1.0, 0.5, key="temp_slider")
|
32 |
-
|
33 |
-
# Reset conversation button
|
34 |
-
st.sidebar.button('Reset Chat', on_click=lambda: (st.session_state.update(conversation=[], messages=[])), key="reset_button")
|
35 |
-
|
36 |
-
# Main task selection: Data Generation or Data Labeling
|
37 |
-
task_choice = st.selectbox("Choose Task", ["Data Generation", "Data Labeling"], key="task_choice_select")
|
38 |
-
|
39 |
-
# Data Generation Section
|
40 |
-
if task_choice == "Data Generation":
|
41 |
-
classification_type = st.selectbox(
|
42 |
-
"Choose Classification Type",
|
43 |
-
["Sentiment Analysis", "Binary Classification", "Multi-Class Classification"],
|
44 |
-
key="classification_type_select"
|
45 |
-
)
|
46 |
-
|
47 |
-
# Define labels based on classification type
|
48 |
-
if classification_type == "Sentiment Analysis":
|
49 |
-
st.session_state.labels = ["Positive", "Negative", "Neutral"]
|
50 |
-
st.write("Sentiment Analysis: Positive, Negative, Neutral")
|
51 |
-
elif classification_type == "Binary Classification":
|
52 |
-
label_1 = st.text_input("Enter first class", key="binary_class_1")
|
53 |
-
label_2 = st.text_input("Enter second class", key="binary_class_2")
|
54 |
-
st.session_state.labels = [label_1, label_2]
|
55 |
-
elif classification_type == "Multi-Class Classification":
|
56 |
-
num_classes = st.slider("How many classes?", 3, 10, 3, key="num_classes_slider")
|
57 |
-
st.session_state.labels = [st.text_input(f"Class {i+1}", key=f"class_input_{i+1}") for i in range(num_classes)]
|
58 |
-
|
59 |
-
# Domain selection
|
60 |
-
domain = st.selectbox("Choose Domain", ["Restaurant reviews", "E-commerce reviews", "Custom"], key="domain_select")
|
61 |
-
if domain == "Custom":
|
62 |
-
domain = st.text_input("Specify custom domain", key="custom_domain_input")
|
63 |
-
|
64 |
-
# Word count selection
|
65 |
-
min_words = st.number_input("Minimum words per example", min_value=10, max_value=90, value=10, key="min_words_input")
|
66 |
-
max_words = st.number_input("Maximum words per example", min_value=10, max_value=90, value=90, key="max_words_input")
|
67 |
-
|
68 |
-
# Few-shot examples option
|
69 |
-
few_shot = st.radio("Do you want to use few-shot examples?", ["Yes", "No"], key="few_shot_radio")
|
70 |
-
if few_shot == "Yes":
|
71 |
-
num_examples = st.slider("How many few-shot examples?", 1, 5, 1, key="num_examples_slider")
|
72 |
-
st.session_state.few_shot_examples = [
|
73 |
-
{
|
74 |
-
"content": st.text_area(f"Example {i+1} Text", key=f"example_text_{i+1}"),
|
75 |
-
"label": st.selectbox(f"Label for Example {i+1}", st.session_state.labels, key=f"label_select_{i+1}")
|
76 |
-
}
|
77 |
-
for i in range(num_examples)
|
78 |
-
]
|
79 |
-
else:
|
80 |
-
st.session_state.few_shot_examples = []
|
81 |
-
|
82 |
-
# Number of examples to generate
|
83 |
-
num_to_generate = st.number_input("How many examples to generate?", min_value=1, max_value=100, value=10, key="num_to_generate_input")
|
84 |
-
|
85 |
-
# User prompt text field
|
86 |
-
user_prompt = st.text_area("Enter your prompt to guide example generation", "", key="user_prompt_text_area")
|
87 |
-
|
88 |
-
# System prompt generation
|
89 |
-
system_prompt = f"You are a professional {classification_type.lower()} expert. Your role is to generate data for {domain}.\n\n"
|
90 |
-
if st.session_state.few_shot_examples:
|
91 |
-
system_prompt += "Use the following few-shot examples as a reference:\n"
|
92 |
-
for example in st.session_state.few_shot_examples:
|
93 |
-
system_prompt += f"Example: {example['content']} \n Label: {example['label']}\n"
|
94 |
-
system_prompt += f"Generate {num_to_generate} unique examples with diverse phrasing.\n"
|
95 |
-
system_prompt += f"Each example should have between {min_words} and {max_words} words.\n"
|
96 |
-
system_prompt += f"Use the labels specified: {', '.join(st.session_state.labels)}.\n"
|
97 |
-
if user_prompt:
|
98 |
-
system_prompt += f"Additional instructions: {user_prompt}\n"
|
99 |
-
|
100 |
-
st.write("System Prompt:")
|
101 |
-
st.code(system_prompt)
|
102 |
-
|
103 |
-
if st.button("Generate Examples", key="generate_examples_button"):
|
104 |
-
# Generate examples by concatenating all inputs and sending it to the model
|
105 |
-
with st.spinner("Generating..."):
|
106 |
-
st.session_state.messages.append({"role": "system", "content": system_prompt})
|
107 |
-
|
108 |
-
try:
|
109 |
-
stream = client.chat.completions.create(
|
110 |
-
model=selected_model,
|
111 |
-
messages=[
|
112 |
-
{"role": m["role"], "content": m["content"]}
|
113 |
-
for m in st.session_state.messages
|
114 |
-
],
|
115 |
-
temperature=temp_values,
|
116 |
-
stream=True,
|
117 |
-
max_tokens=3000,
|
118 |
-
)
|
119 |
-
response = ""
|
120 |
-
for chunk in stream:
|
121 |
-
response += chunk['choices'][0]['delta'].get('content', '')
|
122 |
-
st.write(response)
|
123 |
-
except Exception as e:
|
124 |
-
st.error(f"Error during generation: {e}")
|
125 |
-
|
126 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
127 |
-
|
128 |
-
# Data Labeling Section
|
129 |
-
else:
|
130 |
-
# Classification Type and Labels Setup
|
131 |
-
classification_type = st.selectbox("Choose Classification Type", ["Sentiment Analysis", "Binary Classification", "Multi-Class Classification"], key="classification_type_labeling")
|
132 |
-
|
133 |
-
if classification_type == "Sentiment Analysis":
|
134 |
-
st.session_state.labels = ["Positive", "Negative", "Neutral"]
|
135 |
-
st.write("Sentiment Analysis labels: Positive, Negative, Neutral")
|
136 |
-
elif classification_type == "Binary Classification":
|
137 |
-
label_1 = st.text_input("Enter first class", key="binary_class_1_labeling")
|
138 |
-
label_2 = st.text_input("Enter second class", key="binary_class_2_labeling")
|
139 |
-
st.session_state.labels = [label_1, label_2]
|
140 |
-
elif classification_type == "Multi-Class Classification":
|
141 |
-
num_classes = st.slider("How many classes?", 3, 10, 3, key="num_classes_labeling")
|
142 |
-
st.session_state.labels = [st.text_input(f"Class {i+1}", key=f"class_input_labeling_{i+1}") for i in range(num_classes)]
|
143 |
-
|
144 |
-
# Few-shot examples for labeling
|
145 |
-
use_few_shot = st.radio("Do you want to use few-shot examples?", ["Yes", "No"], key="use_few_shot_labeling")
|
146 |
-
if use_few_shot == "Yes":
|
147 |
-
num_examples = st.slider("How many few-shot examples?", 1, 5, 1, key="few_shot_num_labeling")
|
148 |
-
st.session_state.few_shot_examples = [
|
149 |
-
{
|
150 |
-
"content": st.text_area(f"Example {i+1} Text", key=f"example_text_labeling_{i+1}"),
|
151 |
-
"label": st.selectbox(f"Label for Example {i+1}", st.session_state.labels, key=f"label_select_labeling_{i+1}")
|
152 |
-
}
|
153 |
-
for i in range(num_examples)
|
154 |
-
]
|
155 |
-
else:
|
156 |
-
st.session_state.few_shot_examples = []
|
157 |
-
|
158 |
-
# Input Examples for Classification
|
159 |
-
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")
|
160 |
-
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)]
|
161 |
-
|
162 |
-
# Placeholder for classification function (can be replaced with actual API call)
|
163 |
-
def classify_examples(examples, labels):
|
164 |
-
classified_results = [{"example": ex, "label": random.choice(labels)} for ex in examples]
|
165 |
-
return classified_results
|
166 |
-
|
167 |
-
# Classification results display
|
168 |
-
if st.button("Classify Examples", key="classify_button"):
|
169 |
-
results = classify_examples(st.session_state.examples_to_classify, st.session_state.labels)
|
170 |
-
st.write("Classification Results:")
|
171 |
-
for result in results:
|
172 |
-
st.write(f"Example: {result['example']}\nLabel: {result['label']}\n")
|
173 |
-
شحح
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|