import os
import sys
import requests
import gradio as gr

api_url = "https://api.textsynth.com"
api_key = os.environ["TEXTSYNTH_API_SECRET_KEY"]
api_engine = "gptneox_20B"

def completion(prompt,max_tokens,temperature,top_k,top_p):
    response = requests.post(api_url + "/v1/engines/" + "gptneox_20B" + "/completions", headers = { "Authorization": "Bearer " + api_key }, json = { "prompt": prompt, "max_tokens": max_tokens ,"temperature": temperature,"top_k": top_k,"top_p": top_p })
    resp = response.json()
    if "text" in resp: 
        return prompt + resp["text"]
    else:
        print("ERROR", resp)
        assert False

    if len(sys.argv) <= 1:
       sys.exit(1)

demo = gr.Interface(
       fn=completion, 
       inputs=[
               gr.inputs.Textbox(lines=10,placeholder='Write some code..'),
               gr.inputs.Slider(10,200,10,100,'Max Tokens',False),
               gr.inputs.Slider(0,1.0,0.1,1.0,'temperature',False),
               gr.inputs.Slider(0,50,1,40,'top_k',True),
               gr.inputs.Slider(0,1.0,0.1,0.9,'top_p',True)
       ],
       outputs="text",
       theme='dark-huggingface',
       title='Solo-Coder',
       description='Build by Ansh and ❤️',
       allow_flagging=False,
 
)

if __name__ == "__main__":
    demo.launch()