import os
import random
from huggingface_hub import InferenceClient
import gradio as gr
from datetime import datetime
import agent
from models import models
import requests
import io
import uuid
base_url="https://johann22-chat-diffusion.hf.space/"
loaded_model=[]
for i,model in enumerate(models):
loaded_model.append(gr.load(f'models/{model}'))
print (loaded_model)
now = datetime.now()
date_time_str = now.strftime("%Y-%m-%d %H:%M:%S")
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
model = gr.load("models/dreamlike-art/dreamlike-photoreal-2.0")
history = []
def infer(txt):
return (model(txt))
def format_prompt(message, history):
prompt = ""
for user_prompt, bot_response in history:
prompt += f"[INST] {user_prompt} [/INST]"
prompt += f" {bot_response} "
prompt += f"[INST] {message} [/INST]"
return prompt
def run_gpt(in_prompt,history):
prompt=format_prompt(in_prompt,history)
seed = random.randint(1,1111111111111111)
print (seed)
generate_kwargs = dict(
temperature=1.0,
max_new_tokens=256,
top_p=0.99,
repetition_penalty=1.0,
do_sample=True,
seed=seed,
)
content = agent.GENERATE_PROMPT + prompt
#print(content)
stream = client.text_generation(content, **generate_kwargs, stream=True, details=True, return_full_text=False)
resp = ""
for response in stream:
resp += response.token.text
return resp
def run(purpose,history,model_drop):
#print(purpose)
#print(hist)
task=None
directory="./"
if history:
history=str(history).strip("[]")
if not history:
history = ""
#action_name, action_input = parse_action(line)
out_prompt = run_gpt(
purpose,
history,
)
yield ("",[(purpose,out_prompt)],None)
#out_img = infer(out_prompt)
model=loaded_model[int(model_drop)]
out_img=model(out_prompt)
print(out_img)
url=f'https://johann22-chat-diffusion.hf.space/file={out_img}'
print(url)
uid = uuid.uuid4()
#urllib.request.urlretrieve(image, 'tmp.png')
#out=Image.open('tmp.png')
r = requests.get(url, stream=True)
if r.status_code == 200:
out = Image.open(io.BytesIO(r.content))
yield ("",[(purpose,out_prompt)],out)
#return ("", [(purpose,history)])
################################################
with gr.Blocks() as iface:
gr.HTML("""