Wedyan2023 commited on
Commit
cd136f2
·
verified ·
1 Parent(s): cb5d537

Update app3.py

Browse files
Files changed (1) hide show
  1. app3.py +141 -0
app3.py CHANGED
@@ -1 +1,142 @@
1
  # it work but not well
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # it work but not well
2
+ import numpy as np
3
+ import streamlit as st
4
+ from openai import OpenAI
5
+ import os
6
+ import sys
7
+ from dotenv import load_dotenv
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('HUGGINGFACEHUB_API_TOKEN') # 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=50, value=10)
100
+
101
+ # System prompt generation
102
+ system_prompt = f"You are a professional {classification_type.lower()} expert. Your role is to generate data for {domain}.\n\n"
103
+ if few_shot_examples:
104
+ system_prompt += "Use the following few-shot examples as a reference:\n"
105
+ for example in few_shot_examples:
106
+ system_prompt += f"Example: {example['content']}, Label: {example['label']}\n"
107
+ system_prompt += f"Each example should have between {min_words} and {max_words} words.\n"
108
+ system_prompt += "Think step by step while generating the examples."
109
+
110
+ st.write("System Prompt:")
111
+ st.code(system_prompt)
112
+
113
+ if st.button("Generate Examples"):
114
+ # Generate examples by concatenating all inputs and sending it to the model
115
+ with st.spinner("Generating..."):
116
+ st.session_state.messages.append({"role": "system", "content": system_prompt})
117
+
118
+ try:
119
+ stream = client.chat.completions.create(
120
+ model=model_links[selected_model],
121
+ messages=[
122
+ {"role": m["role"], "content": m["content"]}
123
+ for m in st.session_state.messages
124
+ ],
125
+ temperature=temp_values,
126
+ stream=True,
127
+ max_tokens=3000,
128
+ )
129
+ response = st.write_stream(stream)
130
+ except Exception as e:
131
+ response = "Error during generation."
132
+ random_dog_pick = 'https://random.dog/' + random_dog[np.random.randint(len(random_dog))]
133
+ st.image(random_dog_pick)
134
+ st.write(e)
135
+
136
+ st.session_state.messages.append({"role": "assistant", "content": response})
137
+
138
+ else:
139
+ # Data labeling workflow (for future implementation based on classification)
140
+ st.write("Data Labeling functionality will go here.")
141
+
142
+