PierreBrunelle
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -2,21 +2,19 @@ import gradio as gr
|
|
2 |
import pixeltable as pxt
|
3 |
from pixeltable.functions.mistralai import chat_completions
|
4 |
from datetime import datetime
|
5 |
-
|
6 |
from textblob import TextBlob
|
7 |
import re
|
8 |
import nltk
|
9 |
from nltk.tokenize import word_tokenize
|
10 |
from nltk.corpus import stopwords
|
|
|
|
|
11 |
|
12 |
# Ensure necessary NLTK data is downloaded
|
13 |
nltk.download('punkt', quiet=True)
|
14 |
nltk.download('stopwords', quiet=True)
|
15 |
nltk.download('punkt_tab', quiet=True)
|
16 |
|
17 |
-
import os
|
18 |
-
import getpass
|
19 |
-
|
20 |
# Set up Mistral API key
|
21 |
if 'MISTRAL_API_KEY' not in os.environ:
|
22 |
os.environ['MISTRAL_API_KEY'] = getpass.getpass('Mistral AI API Key:')
|
@@ -42,7 +40,6 @@ def calculate_readability(text: str) -> float:
|
|
42 |
|
43 |
# Function to run inference and analysis
|
44 |
def run_inference_and_analysis(task, system_prompt, input_text, temperature, top_p, max_tokens, min_tokens, stop, random_seed, safe_prompt):
|
45 |
-
|
46 |
# Initialize Pixeltable
|
47 |
pxt.drop_table('mistral_prompts', ignore_errors=True)
|
48 |
t = pxt.create_table('mistral_prompts', {
|
@@ -57,7 +54,7 @@ def run_inference_and_analysis(task, system_prompt, input_text, temperature, top
|
|
57 |
'stop': pxt.StringType(nullable=True),
|
58 |
'random_seed': pxt.IntType(nullable=True),
|
59 |
'safe_prompt': pxt.BoolType(nullable=True)
|
60 |
-
|
61 |
|
62 |
# Insert new row into Pixeltable
|
63 |
t.insert([{
|
@@ -81,14 +78,14 @@ def run_inference_and_analysis(task, system_prompt, input_text, temperature, top
|
|
81 |
]
|
82 |
|
83 |
common_params = {
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
}
|
93 |
|
94 |
# Run inference with both models
|
@@ -116,9 +113,7 @@ def run_inference_and_analysis(task, system_prompt, input_text, temperature, top
|
|
116 |
).tail(1)
|
117 |
|
118 |
history = t.select(t.timestamp, t.task, t.system, t.input_text).order_by(t.timestamp, asc=False).collect().to_pandas()
|
119 |
-
|
120 |
responses = t.select(t.timestamp, t.omn_response, t.ml_response).order_by(t.timestamp, asc=False).collect().to_pandas()
|
121 |
-
|
122 |
analysis = t.select(
|
123 |
t.timestamp,
|
124 |
t.open_sentiment_score,
|
@@ -128,7 +123,6 @@ def run_inference_and_analysis(task, system_prompt, input_text, temperature, top
|
|
128 |
t.open_readability_score,
|
129 |
t.large_readability_score
|
130 |
).order_by(t.timestamp, asc=False).collect().to_pandas()
|
131 |
-
|
132 |
params = t.select(
|
133 |
t.timestamp,
|
134 |
t.temperature,
|
@@ -158,60 +152,60 @@ def run_inference_and_analysis(task, system_prompt, input_text, temperature, top
|
|
158 |
# Gradio interface
|
159 |
def gradio_interface():
|
160 |
with gr.Blocks(theme=gr.themes.Base(), title="Prompt Engineering and LLM Studio") as demo:
|
161 |
-
gr.Markdown(
|
162 |
-
"""
|
163 |
-
<div style= margin-bottom: 20px;">
|
164 |
-
<img src="https://raw.githubusercontent.com/pixeltable/pixeltable/main/docs/source/data/pixeltable-logo-large.png" alt="Pixeltable" style="max-width: 150px;" />
|
165 |
-
<h2>Text and Image similarity search on video frames with embedding indexes</h2>
|
166 |
-
</div>
|
167 |
-
"""
|
168 |
-
)
|
169 |
gr.Markdown(
|
170 |
-
|
171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
|
177 |
-
|
178 |
"""
|
179 |
-
|
180 |
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
|
214 |
-
|
215 |
temperature = gr.Slider(minimum=0, maximum=1, value=0.7, step=0.1, label="Temperature")
|
216 |
top_p = gr.Slider(minimum=0, maximum=1, value=0.9, step=0.1, label="Top P")
|
217 |
max_tokens = gr.Number(label="Max Tokens", value=300)
|
@@ -220,9 +214,9 @@ def gradio_interface():
|
|
220 |
random_seed = gr.Number(label="Random Seed", value=None)
|
221 |
safe_prompt = gr.Checkbox(label="Safe Prompt", value=False)
|
222 |
|
223 |
-
|
224 |
|
225 |
-
|
226 |
with gr.Tab("Prompt Input"):
|
227 |
history = gr.Dataframe(
|
228 |
headers=["Task", "System Prompt", "Input Text", "Timestamp"],
|
@@ -262,59 +256,59 @@ def gradio_interface():
|
|
262 |
"Safe Prompt"
|
263 |
],
|
264 |
wrap=True
|
265 |
-
|
266 |
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
return demo
|
319 |
|
320 |
# Launch the Gradio interface
|
|
|
2 |
import pixeltable as pxt
|
3 |
from pixeltable.functions.mistralai import chat_completions
|
4 |
from datetime import datetime
|
|
|
5 |
from textblob import TextBlob
|
6 |
import re
|
7 |
import nltk
|
8 |
from nltk.tokenize import word_tokenize
|
9 |
from nltk.corpus import stopwords
|
10 |
+
import os
|
11 |
+
import getpass
|
12 |
|
13 |
# Ensure necessary NLTK data is downloaded
|
14 |
nltk.download('punkt', quiet=True)
|
15 |
nltk.download('stopwords', quiet=True)
|
16 |
nltk.download('punkt_tab', quiet=True)
|
17 |
|
|
|
|
|
|
|
18 |
# Set up Mistral API key
|
19 |
if 'MISTRAL_API_KEY' not in os.environ:
|
20 |
os.environ['MISTRAL_API_KEY'] = getpass.getpass('Mistral AI API Key:')
|
|
|
40 |
|
41 |
# Function to run inference and analysis
|
42 |
def run_inference_and_analysis(task, system_prompt, input_text, temperature, top_p, max_tokens, min_tokens, stop, random_seed, safe_prompt):
|
|
|
43 |
# Initialize Pixeltable
|
44 |
pxt.drop_table('mistral_prompts', ignore_errors=True)
|
45 |
t = pxt.create_table('mistral_prompts', {
|
|
|
54 |
'stop': pxt.StringType(nullable=True),
|
55 |
'random_seed': pxt.IntType(nullable=True),
|
56 |
'safe_prompt': pxt.BoolType(nullable=True)
|
57 |
+
})
|
58 |
|
59 |
# Insert new row into Pixeltable
|
60 |
t.insert([{
|
|
|
78 |
]
|
79 |
|
80 |
common_params = {
|
81 |
+
'messages': msgs,
|
82 |
+
'temperature': temperature,
|
83 |
+
'top_p': top_p,
|
84 |
+
'max_tokens': max_tokens if max_tokens is not None else 300,
|
85 |
+
'min_tokens': min_tokens,
|
86 |
+
'stop': stop.split(',') if stop else None,
|
87 |
+
'random_seed': random_seed,
|
88 |
+
'safe_prompt': safe_prompt
|
89 |
}
|
90 |
|
91 |
# Run inference with both models
|
|
|
113 |
).tail(1)
|
114 |
|
115 |
history = t.select(t.timestamp, t.task, t.system, t.input_text).order_by(t.timestamp, asc=False).collect().to_pandas()
|
|
|
116 |
responses = t.select(t.timestamp, t.omn_response, t.ml_response).order_by(t.timestamp, asc=False).collect().to_pandas()
|
|
|
117 |
analysis = t.select(
|
118 |
t.timestamp,
|
119 |
t.open_sentiment_score,
|
|
|
123 |
t.open_readability_score,
|
124 |
t.large_readability_score
|
125 |
).order_by(t.timestamp, asc=False).collect().to_pandas()
|
|
|
126 |
params = t.select(
|
127 |
t.timestamp,
|
128 |
t.temperature,
|
|
|
152 |
# Gradio interface
|
153 |
def gradio_interface():
|
154 |
with gr.Blocks(theme=gr.themes.Base(), title="Prompt Engineering and LLM Studio") as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
gr.Markdown(
|
156 |
+
"""
|
157 |
+
<div style="margin-bottom: 20px;">
|
158 |
+
<img src="https://raw.githubusercontent.com/pixeltable/pixeltable/main/docs/source/data/pixeltable-logo-large.png" alt="Pixeltable" style="max-width: 150px;" />
|
159 |
+
<h2>Text and Image similarity search on video frames with embedding indexes</h2>
|
160 |
+
</div>
|
161 |
+
"""
|
162 |
+
)
|
163 |
+
gr.Markdown(
|
164 |
+
"""
|
165 |
+
# Prompt Engineering and LLM Studio
|
166 |
|
167 |
+
This application demonstrates how [Pixeltable](https://github.com/pixeltable/pixeltable) can be used for rapid and incremental prompt engineering
|
168 |
+
and model comparison workflows. It showcases Pixeltable's ability to directly store, version, index,
|
169 |
+
and transform data while providing an interactive interface to experiment with different prompts and models.
|
170 |
|
171 |
+
Remember, effective prompt engineering often requires experimentation and iteration. Use this tool to systematically improve your prompts and understand how different inputs and parameters affect the LLM outputs.
|
172 |
"""
|
173 |
+
)
|
174 |
|
175 |
+
with gr.Row():
|
176 |
+
with gr.Column():
|
177 |
+
with gr.Accordion("What does it do?", open=False):
|
178 |
+
gr.Markdown(
|
179 |
+
"""
|
180 |
+
1. **Data Organization**: Pixeltable uses tables and views to organize data, similar to traditional databases but with enhanced capabilities for AI workflows.
|
181 |
+
2. **Computed Columns**: These are dynamically generated columns based on expressions applied to columns.
|
182 |
+
3. **Data Storage**: All prompts, responses, and analysis results are stored in Pixeltable tables.
|
183 |
+
4. **Versioning**: Every operations are automatically versioned, allowing you to track changes over time.
|
184 |
+
5. **UDFs**: Sentiment scores, keywords, and readability scores are computed dynamically.
|
185 |
+
6. **Querying**: The history and analysis tabs leverage Pixeltable's querying capabilities to display results.
|
186 |
+
"""
|
187 |
+
)
|
188 |
+
|
189 |
+
with gr.Column():
|
190 |
+
with gr.Accordion("How does it work?", open=False):
|
191 |
+
gr.Markdown(
|
192 |
+
"""
|
193 |
+
1. **Define your task**: This helps you keep track of different experiments.
|
194 |
+
2. **Set up your prompt**: Enter a system prompt in the "System Prompt" field. Write your specific input or question in the "Input Text" field
|
195 |
+
3. **Adjust parameters (optional)**: Adjust temperature, top_p, token limits, etc., to control the model's output.
|
196 |
+
4. **Run the analysis**: Click the "Run Inference and Analysis" button.
|
197 |
+
5. **Review the results**: Compare the responses from both models and exmaine the scores.
|
198 |
+
6. **Iterate and refine**: Based on the results, refine your prompt or adjust parameters.
|
199 |
+
"""
|
200 |
+
)
|
201 |
+
|
202 |
+
with gr.Row():
|
203 |
+
with gr.Column():
|
204 |
+
task = gr.Textbox(label="Task (Arbitrary Category)")
|
205 |
+
system_prompt = gr.Textbox(label="System Prompt")
|
206 |
+
input_text = gr.Textbox(label="Input Text")
|
207 |
|
208 |
+
with gr.Accordion("Advanced Settings", open=False):
|
209 |
temperature = gr.Slider(minimum=0, maximum=1, value=0.7, step=0.1, label="Temperature")
|
210 |
top_p = gr.Slider(minimum=0, maximum=1, value=0.9, step=0.1, label="Top P")
|
211 |
max_tokens = gr.Number(label="Max Tokens", value=300)
|
|
|
214 |
random_seed = gr.Number(label="Random Seed", value=None)
|
215 |
safe_prompt = gr.Checkbox(label="Safe Prompt", value=False)
|
216 |
|
217 |
+
submit_btn = gr.Button("Run Inference and Analysis")
|
218 |
|
219 |
+
with gr.Tabs():
|
220 |
with gr.Tab("Prompt Input"):
|
221 |
history = gr.Dataframe(
|
222 |
headers=["Task", "System Prompt", "Input Text", "Timestamp"],
|
|
|
256 |
"Safe Prompt"
|
257 |
],
|
258 |
wrap=True
|
259 |
+
)
|
260 |
|
261 |
+
# Define the examples
|
262 |
+
examples = [
|
263 |
+
# Example 1: Sentiment Analysis
|
264 |
+
["Sentiment Analysis",
|
265 |
+
"You are an AI trained to analyze the sentiment of text. Provide a detailed analysis of the emotional tone, highlighting key phrases that indicate sentiment.",
|
266 |
+
"The new restaurant downtown exceeded all my expectations. The food was exquisite, the service impeccable, and the ambiance was perfect for a romantic evening. I can't wait to go back!",
|
267 |
+
0.3, 0.95, 200, None, "", None, False],
|
268 |
+
|
269 |
+
# Example 2: Creative Writing
|
270 |
+
["Story Generation",
|
271 |
+
"You are a creative writer. Generate a short, engaging story based on the given prompt. Include vivid descriptions and an unexpected twist.",
|
272 |
+
"In a world where dreams are shared, a young girl discovers she can manipulate other people's dreams.",
|
273 |
+
0.9, 0.8, 500, 200, "The end.", None, False]
|
274 |
+
]
|
275 |
|
276 |
+
with gr.Column():
|
277 |
+
omn_response = gr.Textbox(label="Open-Mistral-Nemo Response")
|
278 |
+
ml_response = gr.Textbox(label="Mistral-Medium Response")
|
279 |
+
|
280 |
+
with gr.Row():
|
281 |
+
large_sentiment = gr.Number(label="Mistral-Medium Sentiment")
|
282 |
+
open_sentiment = gr.Number(label="Open-Mistral-Nemo Sentiment")
|
283 |
+
|
284 |
+
with gr.Row():
|
285 |
+
large_keywords = gr.Textbox(label="Mistral-Medium Keywords")
|
286 |
+
open_keywords = gr.Textbox(label="Open-Mistral-Nemo Keywords")
|
287 |
+
|
288 |
+
with gr.Row():
|
289 |
+
large_readability = gr.Number(label="Mistral-Medium Readability")
|
290 |
+
open_readability = gr.Number(label="Open-Mistral-Nemo Readability")
|
291 |
+
|
292 |
+
gr.Examples(
|
293 |
+
examples=examples,
|
294 |
+
inputs=[task, system_prompt, input_text, temperature, top_p, max_tokens, min_tokens, stop, random_seed, safe_prompt],
|
295 |
+
outputs=[omn_response, ml_response, large_sentiment, open_sentiment, large_keywords, open_keywords, large_readability, open_readability],
|
296 |
+
fn=run_inference_and_analysis,
|
297 |
+
cache_examples=True,
|
298 |
+
)
|
299 |
|
300 |
+
gr.Markdown(
|
301 |
+
"""
|
302 |
+
For more information, visit [Pixeltable's GitHub repository](https://github.com/pixeltable/pixeltable).
|
303 |
+
"""
|
304 |
+
)
|
305 |
+
|
306 |
+
submit_btn.click(
|
307 |
+
run_inference_and_analysis,
|
308 |
+
inputs=[task, system_prompt, input_text, temperature, top_p, max_tokens, min_tokens, stop, random_seed, safe_prompt],
|
309 |
+
outputs=[omn_response, ml_response, large_sentiment, open_sentiment, large_keywords, open_keywords, large_readability, open_readability, history, responses, analysis, params]
|
310 |
+
)
|
311 |
+
|
312 |
return demo
|
313 |
|
314 |
# Launch the Gradio interface
|