import uuid import gradio as gr import re from diffusers.utils import load_image import requests from awesome_chat import chat_huggingface import os os.makedirs("public/images", exist_ok=True) os.makedirs("public/audios", exist_ok=True) os.makedirs("public/videos", exist_ok=True) class Client: def __init__(self) -> None: self.OPENAI_KEY = "" self.HUGGINGFACE_TOKEN = "" self.all_messages = [] def set_key(self, openai_key): self.OPENAI_KEY = openai_key if len(self.HUGGINGFACE_TOKEN)>0: gr.update(visible = True) return self.OPENAI_KEY def set_token(self, huggingface_token): self.HUGGINGFACE_TOKEN = huggingface_token if len(self.OPENAI_KEY)>0: gr.update(visible = True) return self.HUGGINGFACE_TOKEN def add_message(self, content, role): message = {"role":role, "content":content} self.all_messages.append(message) def extract_medias(self, message): image_pattern = re.compile(r"(http(s?):|\/)?([\.\/_\w:-])*?\.(jpg|jpeg|tiff|gif|png)") image_urls = [] for match in image_pattern.finditer(message): if match.group(0) not in image_urls: image_urls.append(match.group(0)) audio_pattern = re.compile(r"(http(s?):|\/)?([\.\/_\w:-])*?\.(flac|wav)") audio_urls = [] for match in audio_pattern.finditer(message): if match.group(0) not in audio_urls: audio_urls.append(match.group(0)) video_pattern = re.compile(r"(http(s?):|\/)?([\.\/_\w:-])*?\.(mp4)") video_urls = [] for match in video_pattern.finditer(message): if match.group(0) not in video_urls: video_urls.append(match.group(0)) return image_urls, audio_urls, video_urls def add_text(self, messages, message): if len(self.OPENAI_KEY) == 0 or not self.OPENAI_KEY.startswith("sk-") or len(self.HUGGINGFACE_TOKEN) == 0 or not self.HUGGINGFACE_TOKEN.startswith("hf_"): return messages, "Please set your OpenAI API key or Hugging Face token first!!!" self.add_message(message, "user") messages = messages + [(message, None)] image_urls, audio_urls, video_urls = self.extract_medias(message) for image_url in image_urls: if not image_url.startswith("http") and not image_url.startswith("public"): image_url = "public/" + image_url image = load_image(image_url) name = f"public/images/{str(uuid.uuid4())[:4]}.jpg" image.save(name) messages = messages + [((f"{name}",), None)] for audio_url in audio_urls and not audio_url.startswith("public"): if not audio_url.startswith("http"): audio_url = "public/" + audio_url ext = audio_url.split(".")[-1] name = f"public/audios/{str(uuid.uuid4()[:4])}.{ext}" response = requests.get(audio_url) with open(name, "wb") as f: f.write(response.content) messages = messages + [((f"{name}",), None)] for video_url in video_urls and not video_url.startswith("public"): if not video_url.startswith("http"): video_url = "public/" + video_url ext = video_url.split(".")[-1] name = f"public/audios/{str(uuid.uuid4()[:4])}.{ext}" response = requests.get(video_url) with open(name, "wb") as f: f.write(response.content) messages = messages + [((f"{name}",), None)] return messages, "" def bot(self, messages): if len(self.OPENAI_KEY) == 0 or not self.OPENAI_KEY.startswith("sk-") or len(self.HUGGINGFACE_TOKEN) == 0 or not self.HUGGINGFACE_TOKEN.startswith("hf_"): return messages message = chat_huggingface(self.all_messages, self.OPENAI_KEY, self.HUGGINGFACE_TOKEN)["message"] image_urls, audio_urls, video_urls = self.extract_medias(message) self.add_message(message, "assistant") messages[-1][1] = message for image_url in image_urls: if not image_url.startswith("http"): image_url = image_url.replace("public/", "") messages = messages + [((None, (f"public/{image_url}",)))] # else: # messages = messages + [((None, (f"{image_url}",)))] for audio_url in audio_urls: if not audio_url.startswith("http"): audio_url = audio_url.replace("public/", "") messages = messages + [((None, (f"public/{audio_url}",)))] # else: # messages = messages + [((None, (f"{audio_url}",)))] for video_url in video_urls: if not video_url.startswith("http"): video_url = video_url.replace("public/", "") messages = messages + [((None, (f"public/{video_url}",)))] # else: # messages = messages + [((None, (f"{video_url}",)))] return messages with gr.Blocks() as demo: state = gr.State(value={"client": Client()}) gr.Markdown("
A system to connect LLMs with ML community. See our Project and Paper.
") gr.HTML('''