Wedyan2023 commited on
Commit
c2ef26c
·
verified ·
1 Parent(s): 9ab72dc

Delete app222.py

Browse files
Files changed (1) hide show
  1. app222.py +0 -161
app222.py DELETED
@@ -1,161 +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
-
8
- os.environ["BROWSER_GATHERUSAGESTATS"] = "false"
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('TOKEN2') # 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
- # Reset conversation
23
- def reset_conversation():
24
- st.session_state.conversation = []
25
- st.session_state.messages = []
26
- return None
27
-
28
- # Define the available models
29
- models = [key for key in model_links.keys()]
30
-
31
- # Sidebar for model selection
32
- selected_model = st.sidebar.selectbox("Select Model", models)
33
-
34
- # Temperature slider with default adjusted for labeling consistency
35
- temp_values = st.sidebar.slider('Select a temperature value', 0.1, 1.0, 0.3)
36
-
37
- # Reset button
38
- st.sidebar.button('Reset Chat', on_click=reset_conversation)
39
-
40
- # Model description
41
- st.sidebar.write(f"You're now chatting with **{selected_model}**")
42
- st.sidebar.markdown("*Generated content may be inaccurate or false.*")
43
-
44
- # Chat initialization
45
- if "messages" not in st.session_state:
46
- st.session_state.messages = []
47
-
48
- # Display chat messages
49
- for message in st.session_state.messages:
50
- with st.chat_message(message["role"]):
51
- st.markdown(message["content"])
52
-
53
- # Main logic to choose between data generation and data labeling
54
- task_choice = st.selectbox("Choose Task", ["Data Generation", "Data Labeling"])
55
-
56
- if task_choice == "Data Generation":
57
- classification_type = st.selectbox(
58
- "Choose Classification Type",
59
- ["Sentiment Analysis", "Binary Classification", "Multi-Class Classification"]
60
- )
61
- if classification_type == "Sentiment Analysis":
62
- st.write("Sentiment Analysis: Positive, Negative, Neutral")
63
- labels = ["Positive", "Negative", "Neutral"]
64
- elif classification_type == "Binary Classification":
65
- label_1 = st.text_input("Enter first class")
66
- label_2 = st.text_input("Enter second class")
67
- labels = [label_1, label_2]
68
- elif classification_type == "Multi-Class Classification":
69
- num_classes = st.slider("How many classes?", 3, 10, 3)
70
- labels = [st.text_input(f"Class {i + 1}") for i in range(num_classes)]
71
- domain = st.selectbox("Choose Domain", ["Restaurant reviews", "E-commerce reviews", "Custom"])
72
- if domain == "Custom":
73
- domain = st.text_input("Specify custom domain")
74
- min_words = st.number_input("Minimum words per example", min_value=10, max_value=90, value=10)
75
- max_words = st.number_input("Maximum words per example", min_value=10, max_value=90, value=90)
76
- few_shot = st.radio("Do you want to use few-shot examples?", ["Yes", "No"])
77
- if few_shot == "Yes":
78
- num_examples = st.slider("How many few-shot examples?", 1, 5, 1)
79
- few_shot_examples = [
80
- {"content": st.text_area(f"Example {i + 1}", key=f"few_shot_{i}"), "label": st.selectbox(f"Label for example {i + 1}", labels, key=f"label_{i}")}
81
- for i in range(num_examples)
82
- ]
83
- else:
84
- few_shot_examples = []
85
-
86
- # Ask the user how many examples they need
87
- num_to_generate = st.number_input("How many examples to generate?", min_value=1, max_value=100, value=10)
88
- # User prompt text field
89
- user_prompt = st.text_area("Enter your prompt to guide example generation", "")
90
- # System prompt generation
91
- system_prompt = f"You are a professional {classification_type.lower()} expert. Your role is to generate data for {domain}.\n\n"
92
- if few_shot_examples:
93
- system_prompt += "Use the following few-shot examples as a reference:\n"
94
- for example in few_shot_examples:
95
- system_prompt += f"Example: {example['content']} \n Label: {example['label']}\n"
96
- system_prompt += f"Generate {num_to_generate} unique examples with diverse phrasing.\n"
97
- system_prompt += f"Each example should have between {min_words} and {max_words} words.\n"
98
- system_prompt += f"Use the labels specified: {', '.join(labels)}.\n"
99
- if user_prompt:
100
- system_prompt += f"Additional instructions: {user_prompt}\n"
101
- st.write("System Prompt:")
102
- st.code(system_prompt)
103
-
104
- if st.button("Generate Examples"):
105
- # Generate examples by concatenating all inputs and sending it to the model
106
- with st.spinner("Generating..."):
107
- st.session_state.messages.append({"role": "system", "content": system_prompt})
108
- try:
109
- stream = client.chat_completions.create(
110
- model=model_links[selected_model],
111
- messages=[{"role": m["role"], "content": m["content"]} for m in st.session_state.messages],
112
- temperature=temp_values,
113
- stream=True,
114
- max_tokens=3000
115
- )
116
- response = ""
117
- for chunk in stream:
118
- response += chunk['choices'][0]['delta']['content']
119
- st.session_state.messages.append({"role": "assistant", "content": response})
120
- st.markdown(response)
121
- except Exception as e:
122
- st.write("Error during generation. Please try again.")
123
- st.write(e)
124
- else:
125
- # Data labeling workflow
126
- st.write("Data Labeling functionality")
127
-
128
- # Initialize session state variables for classification
129
- if "labels" not in st.session_state:
130
- st.session_state.labels = []
131
- if "few_shot_examples" not in st.session_state:
132
- st.session_state.few_shot_examples = []
133
- if "examples_to_classify" not in st.session_state:
134
- st.session_state.examples_to_classify = []
135
-
136
- # Step 1: Classification Type Selection
137
- classification_type = st.selectbox("Choose Classification Type", ["Sentiment Analysis", "Binary Classification", "Multi-Class Classification"])
138
-
139
- # Step 2: Define Labels based on Classification Type
140
- if classification_type == "Sentiment Analysis":
141
- labels = ["Positive", "Negative", "Neutral"]
142
- st.write("Sentiment Analysis labels: Positive, Negative, Neutral")
143
- elif classification_type == "Binary Classification":
144
- label_1 = st.text_input("Enter first class")
145
- label_2 = st.text_input("Enter second class")
146
- if label_1 and label_2:
147
- labels = [label_1, label_2]
148
- else:
149
- labels = []
150
- elif classification_type is "Multi-Class Classification":
151
- num_classes = st.slider("How many classes?", 3, 10, 3)
152
- labels = [st.text_input(f"Class {i + 1}", key=f"multi_class_{i}") for i in range(num_classes)]
153
-
154
- # Save labels to session state
155
- st.session_state.labels = labels
156
-
157
- # Step 3: Few-Shot Examples
158
- use_few_shot = st.radio("Do you want to use few-shot examples?", ["Yes", "No"])
159
- if use_few_shot == "Yes":
160
- num_examples = st.slider("How many few-shot examples?", 1, 5, 1)
161
- st.session_state.few_shot_examples