Wedyan2023 commited on
Commit
88d060e
·
verified ·
1 Parent(s): 73b3081

Delete app5.py

Browse files
Files changed (1) hide show
  1. app5.py +0 -214
app5.py DELETED
@@ -1,214 +0,0 @@
1
- import numpy as np
2
- import streamlit as st
3
- from openai import OpenAI
4
- import os
5
- from dotenv import load_dotenv
6
- import random
7
- os.environ["BROWSER_GATHERUSAGESTATS"] = "false"
8
-
9
- load_dotenv()
10
-
11
- # Initialize the client
12
- client = OpenAI(
13
- base_url="https://api-inference.huggingface.co/v1",
14
- api_key=os.environ.get('LLL') # Add your Huggingface token here
15
- )
16
-
17
- # Supported models
18
- model_links = {
19
- "Meta-Llama-3-8B": "meta-llama/Meta-Llama-3-8B-Instruct"
20
- }
21
-
22
- # Random dog images for error messages
23
- random_dog = [
24
- "0f476473-2d8b-415e-b944-483768418a95.jpg",
25
- "1bd75c81-f1d7-4e55-9310-a27595fa8762.jpg",
26
- "526590d2-8817-4ff0-8c62-fdcba5306d02.jpg",
27
- "1326984c-39b0-492c-a773-f120d747a7e2.jpg"
28
- ]
29
-
30
- # Reset conversation
31
- def reset_conversation():
32
- st.session_state.conversation = []
33
- st.session_state.messages = []
34
- return None
35
-
36
- # Define the available models
37
- models = [key for key in model_links.keys()]
38
-
39
- # Sidebar for model selection
40
- selected_model = st.sidebar.selectbox("Select Model", models)
41
-
42
- # Temperature slider
43
- temp_values = st.sidebar.slider('Select a temperature value', 0.0, 1.0, 0.5)
44
-
45
- # Reset button
46
- st.sidebar.button('Reset Chat', on_click=reset_conversation)
47
-
48
- # Model description
49
- st.sidebar.write(f"You're now chatting with **{selected_model}**")
50
- st.sidebar.markdown("*Generated content may be inaccurate or false.*")
51
-
52
- # Chat initialization
53
- if "messages" not in st.session_state:
54
- st.session_state.messages = []
55
-
56
- # Display chat messages
57
- for message in st.session_state.messages:
58
- with st.chat_message(message["role"]):
59
- st.markdown(message["content"])
60
-
61
- # Main logic to choose between data generation and data labeling
62
- task_choice = st.selectbox("Choose Task", ["Data Generation", "Data Labeling"])
63
-
64
- if task_choice == "Data Generation":
65
- classification_type = st.selectbox(
66
- "Choose Classification Type",
67
- ["Sentiment Analysis", "Binary Classification", "Multi-Class Classification"]
68
- )
69
-
70
- if classification_type == "Sentiment Analysis":
71
- st.write("Sentiment Analysis: Positive, Negative, Neutral")
72
- labels = ["Positive", "Negative", "Neutral"]
73
- elif classification_type == "Binary Classification":
74
- label_1 = st.text_input("Enter first class")
75
- label_2 = st.text_input("Enter second class")
76
- labels = [label_1, label_2]
77
- elif classification_type == "Multi-Class Classification":
78
- num_classes = st.slider("How many classes?", 3, 10, 3)
79
- labels = [st.text_input(f"Class {i+1}") for i in range(num_classes)]
80
-
81
- domain = st.selectbox("Choose Domain", ["Restaurant reviews", "E-commerce reviews", "Custom"])
82
- if domain == "Custom":
83
- domain = st.text_input("Specify custom domain")
84
-
85
- min_words = st.number_input("Minimum words per example", min_value=10, max_value=90, value=10)
86
- max_words = st.number_input("Maximum words per example", min_value=10, max_value=90, value=90)
87
-
88
- few_shot = st.radio("Do you want to use few-shot examples?", ["Yes", "No"])
89
- if few_shot == "Yes":
90
- num_examples = st.slider("How many few-shot examples?", 1, 5, 1)
91
- few_shot_examples = [
92
- {"content": st.text_area(f"Example {i+1}"), "label": st.selectbox(f"Label for example {i+1}", labels)}
93
- for i in range(num_examples)
94
- ]
95
- else:
96
- few_shot_examples = []
97
-
98
- # Ask the user how many examples they need
99
- num_to_generate = st.number_input("How many examples to generate?", min_value=1, max_value=100, value=10)
100
-
101
- # User prompt text field
102
- user_prompt = st.text_area("Enter your prompt to guide example generation", "")
103
-
104
- # System prompt generation
105
- system_prompt = f"You are a professional {classification_type.lower()} expert. Your role is to generate data for {domain}.\n\n"
106
- if few_shot_examples:
107
- system_prompt += "Use the following few-shot examples as a reference:\n"
108
- for example in few_shot_examples:
109
- system_prompt += f"Example: {example['content']} \n Label: {example['label']}\n"
110
- system_prompt += f"Generate {num_to_generate} unique examples with diverse phrasing.\n"
111
- system_prompt += f"Each example should have between {min_words} and {max_words} words.\n"
112
- system_prompt += f"Use the labels specified: {', '.join(labels)}.\n"
113
- if user_prompt:
114
- system_prompt += f"Additional instructions: {user_prompt}\n"
115
-
116
- st.write("System Prompt:")
117
- st.code(system_prompt)
118
-
119
- if st.button("Generate Examples"):
120
- with st.spinner("Generating..."):
121
- st.session_state.messages.append({"role": "system", "content": system_prompt})
122
-
123
- try:
124
- stream = client.chat.completions.create(
125
- model=model_links[selected_model],
126
- messages=[
127
- {"role": m["role"], "content": m["content"]}
128
- for m in st.session_state.messages
129
- ],
130
- temperature=temp_values,
131
- stream=True,
132
- max_tokens=3000,
133
- )
134
- response = st.write_stream(stream)
135
- except Exception as e:
136
- response = "Error during generation."
137
- random_dog_pick = 'https://random.dog/' + random_dog[np.random.randint(len(random_dog))]
138
- st.image(random_dog_pick)
139
- st.write(e)
140
-
141
- st.session_state.messages.append({"role": "assistant", "content": response})
142
-
143
- else: # Data Labeling Process
144
- labeling_classification_type = st.selectbox(
145
- "Choose Classification Type for Labeling",
146
- ["Sentiment Analysis", "Binary Classification", "Multi-Class Classification"]
147
- )
148
-
149
- # Initialize labels based on classification type
150
- if labeling_classification_type == "Sentiment Analysis":
151
- st.write("Sentiment Analysis: Positive, Negative, Neutral")
152
- labeling_labels = ["Positive", "Negative", "Neutral"]
153
- elif labeling_classification_type == "Binary Classification":
154
- labeling_label_1 = st.text_input("Enter first class for labeling")
155
- labeling_label_2 = st.text_input("Enter second class for labeling")
156
- labeling_labels = [labeling_label_1, labeling_label_2]
157
- elif labeling_classification_type == "Multi-Class Classification":
158
- labeling_num_classes = st.slider("How many classes for labeling?", 3, 10, 3)
159
- labeling_labels = [st.text_input(f"Labeling Class {i+1}") for i in range(labeling_num_classes)]
160
-
161
- # Few-shot examples for labeling
162
- labeling_few_shot = st.radio("Do you want to add few-shot examples for labeling?", ["Yes", "No"])
163
- if labeling_few_shot == "Yes":
164
- labeling_num_examples = st.slider("How many few-shot examples for labeling?", 1, 5, 1)
165
- labeling_few_shot_examples = [
166
- {"content": st.text_area(f"Labeling Example {i+1}"),
167
- "label": st.selectbox(f"Label for labeling example {i+1}", labeling_labels)}
168
- for i in range(labeling_num_examples)
169
- ]
170
- else:
171
- labeling_few_shot_examples = []
172
-
173
- # Input for text to classify
174
- text_to_classify = st.text_area("Enter text to classify")
175
-
176
- if st.button("Classify Text"):
177
- if text_to_classify:
178
- # Prepare the system prompt for classification
179
- labeling_system_prompt = f"You are a professional {labeling_classification_type.lower()} expert. "
180
- labeling_system_prompt += f"Classify the following text using these labels: {', '.join(labeling_labels)}.\n\n"
181
-
182
- if labeling_few_shot_examples:
183
- labeling_system_prompt += "Here are some examples for reference:\n"
184
- for example in labeling_few_shot_examples:
185
- labeling_system_prompt += f"Text: {example['content']}\nLabel: {example['label']}\n\n"
186
-
187
- labeling_system_prompt += f"Text to classify: {text_to_classify}\n"
188
- labeling_system_prompt += "Provide your classification in this format: 'Classification: [label]'\n"
189
- labeling_system_prompt += "Also provide a brief explanation for your classification."
190
-
191
- with st.spinner("Classifying..."):
192
- st.session_state.messages.append({"role": "system", "content": labeling_system_prompt})
193
-
194
- try:
195
- stream = client.chat.completions.create(
196
- model=model_links[selected_model],
197
- messages=[
198
- {"role": m["role"], "content": m["content"]}
199
- for m in st.session_state.messages
200
- ],
201
- temperature=temp_values,
202
- stream=True,
203
- max_tokens=1000,
204
- )
205
- response = st.write_stream(stream)
206
- except Exception as e:
207
- response = "Error during classification."
208
- random_dog_pick = 'https://random.dog/' + random_dog[np.random.randint(len(random_dog))]
209
- st.image(random_dog_pick)
210
- st.write(e)
211
-
212
- st.session_state.messages.append({"role": "assistant", "content": response})
213
- else:
214
- st.warning("Please enter text to classify.")