Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files- gradio_app.py +23 -20
gradio_app.py
CHANGED
@@ -14,6 +14,27 @@ LOGO_PATH = "https://huggingface.co/spaces/ahmedheakl/AIN-Arabic-VLM/resolve/mai
|
|
14 |
def get_gradio_url(path: str) -> str:
|
15 |
return f"https://{USERNAME}-{SPACE_NAME}.hf.space/gradio_api/file={path}"
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
def load_chat(
|
18 |
base_url: str,
|
19 |
model: str,
|
@@ -36,25 +57,8 @@ def load_chat(
|
|
36 |
history = history[:TOP_N_HISTORY]
|
37 |
history = ChatInterface._tuples_to_messages(history)
|
38 |
elif len(history) > 0 and isinstance(history[0], dict):
|
39 |
-
|
40 |
-
|
41 |
-
for i, h in enumerate(history):
|
42 |
-
content = h.get("content", [])
|
43 |
-
role = h.get("role", "")
|
44 |
-
ct = []
|
45 |
-
if isinstance(content, tuple):
|
46 |
-
src_path = content[0]
|
47 |
-
ct = [{"type": "image_url", "image_url": {"url": get_gradio_url(src_path)}}]
|
48 |
-
else:
|
49 |
-
ct = [{"type": "text", "text": content}]
|
50 |
-
if merge:
|
51 |
-
hist[-1]["content"].extend(ct)
|
52 |
-
merge = False
|
53 |
-
else:
|
54 |
-
hist.append({"role": role, "content": ct})
|
55 |
-
if i < len(history) - 1 and role == history[i + 1].get("role", ""):
|
56 |
-
merge = True
|
57 |
-
history = hist
|
58 |
files = message.get('files', [])
|
59 |
content = [
|
60 |
{"type": "text", "text": message.get('text', '')}
|
@@ -63,7 +67,6 @@ def load_chat(
|
|
63 |
src_path = files[0]
|
64 |
content.append({"type": "image_url", "image_url": {"url": get_gradio_url(src_path)}})
|
65 |
messages = history + [{"role": "user", "content": content}]
|
66 |
-
print(messages)
|
67 |
stream = client.chat.completions.create(
|
68 |
model=model,
|
69 |
messages=messages,
|
|
|
14 |
def get_gradio_url(path: str) -> str:
|
15 |
return f"https://{USERNAME}-{SPACE_NAME}.hf.space/gradio_api/file={path}"
|
16 |
|
17 |
+
def history_to_messages(history: list) -> list:
|
18 |
+
messages = []
|
19 |
+
merge = False
|
20 |
+
for i, h in enumerate(history):
|
21 |
+
content = h.get("content", [])
|
22 |
+
role = h.get("role", "")
|
23 |
+
ct = []
|
24 |
+
if isinstance(content, tuple):
|
25 |
+
src_path = content[0]
|
26 |
+
ct = [{"type": "image_url", "image_url": {"url": get_gradio_url(src_path)}}]
|
27 |
+
else:
|
28 |
+
ct = [{"type": "text", "text": content}]
|
29 |
+
if merge:
|
30 |
+
messages[-1]["content"].extend(ct)
|
31 |
+
merge = False
|
32 |
+
else:
|
33 |
+
messages.append({"role": role, "content": ct})
|
34 |
+
if i < len(history) - 1 and role == history[i + 1].get("role", ""):
|
35 |
+
merge = True
|
36 |
+
return messages
|
37 |
+
|
38 |
def load_chat(
|
39 |
base_url: str,
|
40 |
model: str,
|
|
|
57 |
history = history[:TOP_N_HISTORY]
|
58 |
history = ChatInterface._tuples_to_messages(history)
|
59 |
elif len(history) > 0 and isinstance(history[0], dict):
|
60 |
+
history = history_to_messages(history)
|
61 |
+
history = history[:TOP_N_HISTORY]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
files = message.get('files', [])
|
63 |
content = [
|
64 |
{"type": "text", "text": message.get('text', '')}
|
|
|
67 |
src_path = files[0]
|
68 |
content.append({"type": "image_url", "image_url": {"url": get_gradio_url(src_path)}})
|
69 |
messages = history + [{"role": "user", "content": content}]
|
|
|
70 |
stream = client.chat.completions.create(
|
71 |
model=model,
|
72 |
messages=messages,
|