Spaces:
Runtime error
Runtime error
Jin Ziqi
commited on
Commit
·
ee0fa6a
1
Parent(s):
65db038
Add application file
Browse files- app.py +113 -1
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -1,7 +1,119 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
def greet(name):
|
| 4 |
return "Hello " + name + "!!"
|
| 5 |
|
| 6 |
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 7 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from statistics import mean
|
| 3 |
+
from torch.utils.data import Dataset
|
| 4 |
+
from collections import OrderedDict
|
| 5 |
+
import xml.etree.ElementTree as ET
|
| 6 |
+
import openai # For GPT-3 API ...
|
| 7 |
+
import os
|
| 8 |
+
import multiprocessing
|
| 9 |
+
import json
|
| 10 |
+
import numpy as np
|
| 11 |
+
import random
|
| 12 |
+
import torch
|
| 13 |
+
import torchtext
|
| 14 |
+
import re
|
| 15 |
+
import random
|
| 16 |
+
import time
|
| 17 |
+
import datetime
|
| 18 |
+
import pandas as pd
|
| 19 |
+
import sys
|
| 20 |
|
| 21 |
def greet(name):
|
| 22 |
return "Hello " + name + "!!"
|
| 23 |
|
| 24 |
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 25 |
+
iface.launch()
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# Sentence Generator (Decoder) for GPT-3 ...
|
| 29 |
+
def decoder_for_gpt3(args, input, max_length, i, k, filename):
|
| 30 |
+
|
| 31 |
+
# GPT-3 API allows each users execute the API within 60 times in a minute ...
|
| 32 |
+
# time.sleep(1)
|
| 33 |
+
time.sleep(args.api_time_interval)
|
| 34 |
+
|
| 35 |
+
# https://beta.openai.com/account/api-keys
|
| 36 |
+
# api_list = []
|
| 37 |
+
# with open(filename,"r") as f:
|
| 38 |
+
# api_list = [line.rstrip('\n') for line in f]
|
| 39 |
+
# first_api = api_list[0]
|
| 40 |
+
# api_list = api_list[1:]
|
| 41 |
+
# api_list.append(first_api)
|
| 42 |
+
openai.api_key = "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"
|
| 43 |
+
|
| 44 |
+
#print(openai.api_key)
|
| 45 |
+
|
| 46 |
+
# Specify engine ...
|
| 47 |
+
# Instruct GPT3
|
| 48 |
+
if args.model == "gpt3":
|
| 49 |
+
engine = "text-ada-001"
|
| 50 |
+
elif args.model == "gpt3-medium":
|
| 51 |
+
engine = "text-babbage-001"
|
| 52 |
+
elif args.model == "gpt3-large":
|
| 53 |
+
engine = "code-cushman-001"
|
| 54 |
+
elif args.model == "gpt3-xl": # Please change this back
|
| 55 |
+
engine = "code-davinci-002"
|
| 56 |
+
elif args.model == "chatgpt":
|
| 57 |
+
engine = "gpt-3.5-turbo-0301"
|
| 58 |
+
else:
|
| 59 |
+
raise ValueError("model is not properly defined ...")
|
| 60 |
+
cnt = 0
|
| 61 |
+
while True:
|
| 62 |
+
try:
|
| 63 |
+
if args.model=="chatgpt":
|
| 64 |
+
response = openai.ChatCompletion.create(
|
| 65 |
+
model="gpt-3.5-turbo",
|
| 66 |
+
messages=[
|
| 67 |
+
{"role": "system", "content": "You are a helpful assistant that generate table to solve reasoning problem."},
|
| 68 |
+
{"role": "user", "content": input},
|
| 69 |
+
|
| 70 |
+
]
|
| 71 |
+
)
|
| 72 |
+
print(response)
|
| 73 |
+
return response["choices"][0]["message"]["content"]
|
| 74 |
+
# response = openai.Completion.create(
|
| 75 |
+
# engine=engine,
|
| 76 |
+
# prompt=input,
|
| 77 |
+
# max_tokens=max_length,
|
| 78 |
+
# temperature=0,
|
| 79 |
+
# stop='\n\n'
|
| 80 |
+
# )
|
| 81 |
+
# return response["choices"][0]["text"]
|
| 82 |
+
except KeyboardInterrupt:
|
| 83 |
+
print('Interrupted')
|
| 84 |
+
try:
|
| 85 |
+
sys.exit(0)
|
| 86 |
+
except SystemExit:
|
| 87 |
+
os._exit(0)
|
| 88 |
+
except openai.error.RateLimitError:
|
| 89 |
+
time.sleep(3)
|
| 90 |
+
cnt += 1
|
| 91 |
+
print(cnt)
|
| 92 |
+
if cnt >= 10:
|
| 93 |
+
print("!!!Error Occur " + input)
|
| 94 |
+
print("Unexpected error:", sys.exc_info()[0])
|
| 95 |
+
print(len(api_list))
|
| 96 |
+
api_list.remove(api_list[0])
|
| 97 |
+
with open(filename, 'w') as f:
|
| 98 |
+
for s in api_list:
|
| 99 |
+
f.write(s + '\n')
|
| 100 |
+
continue
|
| 101 |
+
|
| 102 |
+
except openai.error.ServiceUnavailableError:
|
| 103 |
+
time.sleep(2)
|
| 104 |
+
continue
|
| 105 |
+
except openai.error.APIConnectionError:
|
| 106 |
+
time.sleep(2)
|
| 107 |
+
continue
|
| 108 |
+
except openai.error.APIError:
|
| 109 |
+
time.sleep(2)
|
| 110 |
+
continue
|
| 111 |
+
except:
|
| 112 |
+
print("!!!Error Occur " + input)
|
| 113 |
+
print("Unexpected error:", sys.exc_info()[0])
|
| 114 |
+
print(len(api_list))
|
| 115 |
+
api_list.remove(api_list[0])
|
| 116 |
+
with open(filename, 'w') as f:
|
| 117 |
+
for s in api_list:
|
| 118 |
+
f.write(s + '\n')
|
| 119 |
+
continue
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
openai==0.18.0
|