wop's picture
Update app.py
08b4749 verified
import gradio as gr
import difflib
# Sample dataset as a single string
dataset_str = """
say "Hello World!" [END]
permission "request" "account"
permission "get"
if permissions.contains("account") (
pass = "what do you want your password to be?".ask.to("md5")
network "update" "password" pass
) [END]
def "factorial" "n"
result = 1
count = 0
loop n (
count ++
result *= count
)
return = result
endef
number = 6
factorial number
say "Factorial of" + number + "is" + return [END]
permission "request" "File Admin"
count = 0
filelength = 0
loop 500 (
count += 1
file "open" "id" count
filelength += file.join(" ").len
)
say "Your drive is" + filelength + "characters long" [END]
txt = ""
loop 9999 (
txt = txt ++ newline ++ [0,999999999].random.to("md5")
)
clipboard "set" txt [END]
people_x = "array".new(100)
people_y = "array".new(100)
pd = "array".new(100)
count = 0
loop 100 (
count += 1
people_x.[count] = 0
people_y.[count] = 0
pd.[count] = 0
)
mainloop:
count = 0
c #fff
loop 20 (
count += 1
goto people_x.[count].destr people_y.[count].destr
icon "w 4 dot 0 0" 1
d = [0,0].dist(x_position,y_position)
if d.int >= 100 "pd.[count] += 180"
people_x.[count] += pd.[count].destr.sin * ( d / 10 + 1 )
people_y.[count] += pd.[count].destr.cos * ( d / 10 + 1 )
pd.[count] += [-10,10].random
)
import "win-buttons" [END]
calc = ["0","","0"]
window "show"
window "resizable" false
window "dimensions" 300 400
def "drawrow"
c = 0
loop row.len (
c += 1
square 50 50 10 : chx#-10 chy#-70 c#222
text row.[c] 20 : c#fff chx#-10
)
endef
mainloop:
loc 999 2 0 -20
square window_width 30 10 : c#222
loc -2 2 -20 -20
icon "close" 0.8 : c#fff
if clicked "window stop"
loc 2 2 10 -20
text "Calculator" 10
displ = calc.[1]
if calc.[2] != "" "displ = displ + calc.[2].destr"
row = "147."
loc 2 2 45 -75
drawrow
row = "2580"
loc 2 2 115 -75
drawrow
row = "369"
loc 2 2 185 -75
drawrow
c2 = calc.[2].destr
row = "+-*/"
loc 2 2 255 -75
c = 0
loop row.len (
c += 1
r = row.[c]
c #333
if c2.destr == r "c #f79204"
square 50 50 10 : chx#-10 chy#-70
if onclick (
calc.[2] = r
if c2 == r "calc.[2] = null"
)
text r 20 : c#fff chx#-10
)
row = "369="
loc 2 2 185 -75
drawrow
loc 2 2 20 -70
text displ 20 : c#fff [END]
"""
# Convert the dataset string to a list of words
dataset = dataset_str.split()
# Function to correct misspelled word
def correct_word(input_word):
matches = difflib.get_close_matches(input_word, dataset)
corrected_word = matches[0] if matches else input_word
return corrected_word
# Function to predict using difflib
def predict(input_word):
matches = difflib.get_close_matches(input_word, dataset)
next_word = matches[0] if matches else "[?]"
if next_word != "[?]":
index = dataset.index(next_word)
next_word = dataset[index + 1] if index < len(dataset) - 1 else "[?]"
return next_word
# Function to generate text
def generate_text(input_text, num_predictions=3):
input_word = input_text.split()[-1].lower() # Convert input to lowercase and take the last word
corrected_input = correct_word(input_word) # Correct the input word
predictions = []
for _ in range(num_predictions):
prediction = predict(input_word)
predictions.append(prediction)
if prediction == "[?]": # Stop generating if no more predictions are possible
break
input_word = prediction # Update input word for next prediction
aa = " ".join(predictions)
answer = corrected_input + " " + aa
return answer
additional_inputs=[
gr.Slider(
label="Max Next Tokens",
value=2,
minimum=1,
maximum=10,
step=1,
interactive=True,
info="Lower values are recomended.",
)
]
# Create interface
iface = gr.Interface(
fn=generate_text,
inputs="text",
outputs="text",
title="OSL Code Completion",
description="Version: 0.3",
live=True,
additional_inputs=additional_inputs,
)
# Launch interface
iface.launch()