Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Here is an improved and fixed version of the `app.py` script with better structure, error handling, and documentation:
|
| 2 |
+
|
| 3 |
+
```python
|
| 4 |
+
import uuid
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import re
|
| 7 |
+
from diffusers.utils import load_image
|
| 8 |
+
import requests
|
| 9 |
+
from awesome_chat import chat_huggingface
|
| 10 |
+
import os
|
| 11 |
+
|
| 12 |
+
# Create necessary directories
|
| 13 |
+
os.makedirs("public/images", exist_ok=True)
|
| 14 |
+
os.makedirs("public/audios", exist_ok=True)
|
| 15 |
+
os.makedirs("public/videos", exist_ok=True)
|
| 16 |
+
|
| 17 |
+
# Fetch environment variables
|
| 18 |
+
HUGGINGFACE_TOKEN = os.environ.get("HUGGINGFACE_TOKEN")
|
| 19 |
+
OPENAI_KEY = os.environ.get("OPENAI_KEY")
|
| 20 |
+
|
| 21 |
+
class Client:
|
| 22 |
+
def __init__(self) -> None:
|
| 23 |
+
self.OPENAI_KEY = OPENAI_KEY
|
| 24 |
+
self.HUGGINGFACE_TOKEN = HUGGINGFACE_TOKEN
|
| 25 |
+
self.all_messages = []
|
| 26 |
+
|
| 27 |
+
def set_key(self, openai_key):
|
| 28 |
+
self.OPENAI_KEY = openai_key
|
| 29 |
+
return self.OPENAI_KEY
|
| 30 |
+
|
| 31 |
+
def set_token(self, huggingface_token):
|
| 32 |
+
self.HUGGINGFACE_TOKEN = huggingface_token
|
| 33 |
+
return self.HUGGINGFACE_TOKEN
|
| 34 |
+
|
| 35 |
+
def add_message(self, content, role):
|
| 36 |
+
message = {"role": role, "content": content}
|
| 37 |
+
self.all_messages.append(message)
|
| 38 |
+
|
| 39 |
+
def extract_medias(self, message):
|
| 40 |
+
url_pattern = re.compile(r"(http(s?):|\/)?([\.\/_\w:-])*?\.(jpg|jpeg|tiff|gif|png|flac|wav|mp4)")
|
| 41 |
+
urls = [match.group(0) for match in url_pattern.finditer(message)]
|
| 42 |
+
|
| 43 |
+
image_pattern = re.compile(r"(http(s?):|\/)?([\.\/_\w:-])*?\.(jpg|jpeg|tiff|gif|png)")
|
| 44 |
+
audio_pattern = re.compile(r"(http(s?):|\/)?([\.\/_\w:-])*?\.(flac|wav)")
|
| 45 |
+
video_pattern = re.compile(r"(http(s?):|\/)?([\.\/_\w:-])*?\.(mp4)")
|
| 46 |
+
|
| 47 |
+
image_urls = [match.group(0) for match in image_pattern.finditer(message)]
|
| 48 |
+
audio_urls = [match.group(0) for match in audio_pattern.finditer(message)]
|
| 49 |
+
video_urls = [match.group(0) for match in video_pattern.finditer(message)]
|
| 50 |
+
|
| 51 |
+
return urls, image_urls, audio_urls, video_urls
|
| 52 |
+
|
| 53 |
+
def add_text(self, messages, message):
|
| 54 |
+
if not self.OPENAI_KEY or not self.OPENAI_KEY.startswith("sk-") or not self.HUGGINGFACE_TOKEN or not self.HUGGINGFACE_TOKEN.startswith("hf_"):
|
| 55 |
+
return messages, "Please set your OpenAI API key and Hugging Face token first!"
|
| 56 |
+
|
| 57 |
+
self.add_message(message, "user")
|
| 58 |
+
messages.append((message, None))
|
| 59 |
+
urls, image_urls, audio_urls, video_urls = self.extract_medias(message)
|
| 60 |
+
|
| 61 |
+
for image_url in image_urls:
|
| 62 |
+
image_url = "public/" + image_url if not image_url.startswith("http") else image_url
|
| 63 |
+
image = load_image(image_url)
|
| 64 |
+
name = f"public/images/{str(uuid.uuid4())[:4]}.jpg"
|
| 65 |
+
image.save(name)
|
| 66 |
+
messages.append(((f"{name}",), None))
|
| 67 |
+
|
| 68 |
+
for audio_url in audio_urls:
|
| 69 |
+
audio_url = "public/" + audio_url if not audio_url.startswith("http") else audio_url
|
| 70 |
+
ext = audio_url.split(".")[-1]
|
| 71 |
+
name = f"public/audios/{str(uuid.uuid4())[:4]}.{ext}"
|
| 72 |
+
response = requests.get(audio_url)
|
| 73 |
+
with open(name, "wb") as f:
|
| 74 |
+
f.write(response.content)
|
| 75 |
+
messages.append(((f"{name}",), None))
|
| 76 |
+
|
| 77 |
+
for video_url in video_urls:
|
| 78 |
+
video_url = "public/" + video_url if not video_url.startswith("http") else video_url
|
| 79 |
+
ext = video_url.split(".")[-1]
|
| 80 |
+
name = f"public/videos/{str(uuid.uuid4())[:4]}.{ext}"
|
| 81 |
+
response = requests.get(video_url)
|
| 82 |
+
with open(name, "wb") as f:
|
| 83 |
+
f.write(response.content)
|
| 84 |
+
messages.append(((f"{name}",), None))
|
| 85 |
+
|
| 86 |
+
return messages, ""
|
| 87 |
+
|
| 88 |
+
def bot(self, messages):
|
| 89 |
+
if not self.OPENAI_KEY or not self.OPENAI_KEY.startswith("sk-") or not self.HUGGINGFACE_TOKEN or not self.HUGGINGFACE_TOKEN.startswith("hf_"):
|
| 90 |
+
return messages, {}
|
| 91 |
+
|
| 92 |
+
message, results = chat_huggingface(self.all_messages, self.OPENAI_KEY, self.HUGGINGFACE_TOKEN)
|
| 93 |
+
_, image_urls, audio_urls, video_urls = self.extract_medias(message)
|
| 94 |
+
self.add_message(message, "assistant")
|
| 95 |
+
messages[-1][1] = message
|
| 96 |
+
|
| 97 |
+
for image_url in image_urls:
|
| 98 |
+
if not image_url.startswith("http"):
|
| 99 |
+
image_url = "public/" + image_url.replace("public/", "")
|
| 100 |
+
messages.append((None, f
|