File size: 1,196 Bytes
9b4edaf
 
 
 
 
 
 
 
201cfef
 
 
9b4edaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import requests
import json
from io import StringIO
import sys
import gradio
import os


def generate_response(query, top_p):
    endpoint = os.environ.get("ENDPOINT", None)
    token = os.environ.get("TOKEN", None)
    params = {
        'accept': 'application/json',
        'Content-Type': 'application/json',
        "Authorization": f"Bearer {token}"
    }
    data = {
        'inputs': query,
        "parameters": {
            "details": True,
            "do_sample": True,
            "max_new_tokens": 256,
            "repetition_penalty": 1.2,
            "seed": 0,
            "stop": ['<|endoftext|>'],
            "temperature": 0.2,
            "top_p": top_p,
        }
    }
    try:
        res = requests.post(endpoint, data=json.dumps(data), headers=params)
        res = json.loads(res.content.decode("utf-8"))
        return res[0]["generated_text"].split("<|endoftext|>")[0].strip()
    except:
        raise gradio.Error("Connection Error. Please check your ACCESS_TOKEN")


def run_code(code):
    output_buffer = StringIO()
    sys.stdout = output_buffer
    exec(code, {})
    sys.stdout = sys.__stdout__
    pred = output_buffer.getvalue().strip()
    return pred