import openai
import gradio as gr
import os
import numpy as np
# Ensure you have set your OpenAI API key as an environment variable
openai.api_key = os.getenv("OPENAI_API_KEY")
client = openai.OpenAI()
def logprob_to_percentage(logprob):
"""Convert log probability to percentage."""
prob = np.exp(logprob)
return prob * 100
def highlight_tokens_with_cumulative(cumulative, token, colors, color_idx):
"""Combine cumulative sentence with a highlighted token using background color."""
highlighted_token = f"{repr(token)}"
return f"{cumulative} {highlighted_token}"
def compute_logprobs(input_text):
try:
output_html = """
"""
colors = [
"#CCCCCC"
#"#FF5733", # Vibrant Red-Orange
#"#FFBD33", # Vibrant Orange
#"#33FF57", # Vibrant Green
#"#3357FF", # Vibrant Blue
#"#57FF33", # Vibrant Lime
#"#FF33A1", # Vibrant Pink
]
# Process the sentence, starting from full sentence down to first token
steps = len(input_text.split())
for i in range(steps, 0, -1):
sub_prompt = " ".join(input_text.split()[:i])
response = client.completions.create(
model="gpt-3.5-turbo-instruct",
prompt=sub_prompt,
max_tokens=1,
logprobs=5
)
# Extract the first completion choice
choice = response.choices[0]
print(choice)
# Prepare the table without headers
table_html = "
"
# Extract and format probabilities
top_predictions = choice.logprobs.top_logprobs[0].items()
color_idx = 0
for token, logprob in top_predictions:
probability = logprob_to_percentage(logprob)
combined = highlight_tokens_with_cumulative(sub_prompt, token, colors, color_idx)
table_html += f"| {combined} | {probability:.2f}% |
"
color_idx = (color_idx + 1) % len(colors)
table_html += "
"
# Append results for the current step
output_html += f"
Eingabe: {sub_prompt}
"
output_html += table_html
output_html += "
"
output_html += "
"
return output_html
except Exception as e:
print(f"Error: {e}")
pass
# Set up Gradio using Blocks for flexibility with custom theme
with gr.Blocks() as demo:
gr.Markdown("## Wahrscheinlichkeiten von Token-Vorhersagen")
gr.Markdown("Satz eingeben um die Wahrscheinlichkeiten der jeweils nächsten Token zu generieren")
input_box = gr.Textbox(label="", placeholder="Etwas tippen...", lines=1)
output_html = gr.HTML(label="Wahrscheinlichkeiten")
# Link function to button and input
input_box.change(compute_logprobs, inputs=input_box, outputs=output_html)
# Launch the Gradio app without flagging
demo.launch()