Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -29,8 +29,40 @@ import aiohttp
|
|
| 29 |
import requests
|
| 30 |
#from textgen import getTextGen,multiprocessPrompts
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
"""
|
| 35 |
Python subprocess non-blocking and non-breaking communicate.: https://gist.github.com/hzhu212/02d5d4845027411b6705f0dc19181ef5
|
| 36 |
|
|
|
|
| 29 |
import requests
|
| 30 |
#from textgen import getTextGen,multiprocessPrompts
|
| 31 |
|
| 32 |
+
def getTextGen(prompt, model=''):
|
| 33 |
+
from huggingface_hub import InferenceClient
|
| 34 |
+
client = InferenceClient(token=os.getenv('inference'))
|
| 35 |
+
import time
|
| 36 |
+
good = False
|
| 37 |
+
response = ''
|
| 38 |
+
tries = 0
|
| 39 |
+
model = model if model else "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
| 40 |
+
while not good and tries<=3:
|
| 41 |
+
try:
|
| 42 |
+
print('getTextGen -- trying prompt: ', model, prompt[:50])
|
| 43 |
+
response = client.text_generation(prompt, model=model, max_new_tokens=2500, temperature=.7)
|
| 44 |
+
print('getTextGen -- generated: ', response[:50], '\n\n')
|
| 45 |
+
good = True
|
| 46 |
+
except:
|
| 47 |
+
time.sleep(.7)
|
| 48 |
+
model = ["mistralai/Mistral-7B-Instruct-v0.2", "microsoft/Phi-3-mini-4k-instruct", "google/gemma-7b"][tries]
|
| 49 |
+
tries += 1
|
| 50 |
+
print('getTextGen -- retrying prompt: ', model, prompt[:50])
|
| 51 |
+
|
| 52 |
+
return response
|
| 53 |
|
| 54 |
|
| 55 |
+
def multiprocessPrompts(prompts):
|
| 56 |
+
from multiprocessing import Pool
|
| 57 |
+
import time
|
| 58 |
+
start = time.time()
|
| 59 |
+
with Pool(5) as p:
|
| 60 |
+
responses = p.map(getTextGen, prompts)
|
| 61 |
+
print(time.time()-start)
|
| 62 |
+
return responses
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
"""
|
| 67 |
Python subprocess non-blocking and non-breaking communicate.: https://gist.github.com/hzhu212/02d5d4845027411b6705f0dc19181ef5
|
| 68 |
|