Spaces:
Runtime error
Runtime error
File size: 5,138 Bytes
a18bcc6 885a8c4 a18bcc6 885a8c4 a18bcc6 b46baae |
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# Copyright 2023 https://github.com/ShishirPatil/gorilla
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import gradio as gr
import openai
import re
# There is no need for an API key let the following be as is
openai.api_key = "EMPTY"
# Set up the API base
openai.api_base = "http://zanino.millennium.berkeley.edu:8000/v1"
# If there is any issue try using
# openai.api_base = "http://34.132.127.197:8000/v1"
# Define function to get Gorilla response
def get_gorilla_response(prompt, model="gorilla-7b-hf-v1"):
completion = openai.ChatCompletion.create(
model=model,
messages=[{"role": "user", "content": prompt}]
)
return completion.choices[0].message.content
# Define function to parse output
def parse_output(text, model):
if model == "gorilla-7b-hf-v1":
components = {}
components['domain'] = text.split("<<<domain>>>:")[1].split("<<<api_call>>>")[0].strip()
components['api_call'] = text.split("<<<api_call>>>:")[1].split("<<<api_provider>>>")[0].strip()
components['api_provider'] = text.split("<<<api_provider>>>:")[1].split("<<<explanation>>>")[0].strip()
components['explanation'] = text.split("<<<explanation>>>:")[1].split("<<<code>>>")[0].strip()
components['code'] = text.split("<<<code>>>:")[1].strip()
return components
elif model == "gorilla-mpt-7b-hf-v0":
keys_to_remove = ['api_call', 'api_provider', 'explanation', 'code']
x = text.split(":")
x.pop(0)
for i in range(len(x)):
for key in keys_to_remove:
x[i] = x[i].replace(f'{key}','').replace(f", '{key}'", '').replace(f", '{key}':", '').replace(f"'{key}':", '').replace('''\\"''','''"''').replace('''"\\''','''"''').replace("""\'""","""'""").replace("""'\\""","""'""")
components = {
'domain': x[0].strip("' ").replace("\n<<<","").replace('"','').replace('<','').replace('>',''),
'api_call': x[1].strip("' ").replace("\n<<<","").replace('<','').replace('>',''),
'api_provider': x[2].strip("' ").replace("\n<<","").replace('<','').replace('>',''),
'explanation': x[3].strip("' ").replace(r'\n', '\n').replace('<','').replace('>',''),
'code': x[4].strip("' ").replace(r'\n', '\n').replace('<','').replace('>','')
}
return components
elif model == "gorilla-7b-th-v0":
x = text.split(":")
keys_to_remove = ['api_call', 'api_provider', 'explanation', 'code']
x.pop(0)
for i in range(len(x)):
for key in keys_to_remove:
x[i] = x[i].replace(f", '{key}'", '').replace(f", '{key}':", '').replace(f"'{key}':", '').replace('''\\"''','''"''').replace('''"\\''','''"''').replace("""\'""","""'""").replace("""'\\""","""'""")
components = {
'domain': x[0].strip("' "),
'api_call': x[1].strip("' "),
'api_provider': x[2].strip("' "),
'explanation': x[3].strip("' ").replace(r'\n', '\n'),
'code': x[4].strip("' ").replace(r'\n', '\n')
}
return components
# Define the function for the interface
def parse_and_display(prompt, model):
text = get_gorilla_response(prompt, model)
components = parse_output(text, model)
domain = components['domain']
api_call = components['api_call']
api_provider = components['api_provider']
explanation = components['explanation']
code = components['code']
return domain, api_call, api_provider, explanation, code
# Define example prompts
examples = [
["I would like to translate 'I feel very good today.' from English to French.","gorilla-7b-hf-v1"],
["I want to build a robot that can detecting objects in an image ‘cat.jpeg’. Input: [‘cat.jpeg’]","gorilla-7b-hf-v1"],
["I would like to translate from English to Chinese.","gorilla-7b-th-v0"],
]
# Create the Gradio interface
iface = gr.Interface(
fn=parse_and_display,
inputs=["text", gr.components.Dropdown(["gorilla-7b-hf-v1", "gorilla-7b-th-v0", "gorilla-7b-tf-v0", "gorilla-mpt-7b-hf-v0"], label="Model")],
outputs=[
gr.components.Textbox(label="Domain"),
gr.components.Textbox(label="API Call"),
gr.components.Textbox(label="API Provider"),
gr.components.Textbox(label="Explanation"),
gr.components.Code(label="Code")
],
title="Gorilla Gradio Explorer",
description="Gorilla is an LLM that can pick the right API for your tasks. Check out the examples below. Learn more at gorilla.cs.berkeley.edu",
examples=examples,
)
# Launch the interface and get the public gradio link
iface.launch() |