Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,8 @@
|
|
| 1 |
-
python id="
|
| 2 |
from transformers import AutoModel, AutoTokenizer
|
| 3 |
import gradio as gr
|
| 4 |
from PIL import Image
|
| 5 |
import time
|
| 6 |
-
import pytesseract
|
| 7 |
-
from PyPDF2 import PdfReader
|
| 8 |
-
import speech_recognition as sr
|
| 9 |
|
| 10 |
# ======================================================
|
| 11 |
# MODEL
|
|
@@ -69,22 +66,22 @@ If you cannot do something, explain clearly and suggest alternatives.
|
|
| 69 |
"""
|
| 70 |
|
| 71 |
# ======================================================
|
| 72 |
-
# MEMORY (
|
| 73 |
# ======================================================
|
| 74 |
|
| 75 |
memory = {}
|
| 76 |
|
| 77 |
def get_memory(user_id):
|
| 78 |
-
return "\n".join(memory.get(user_id, []))
|
| 79 |
|
| 80 |
def save_memory(user_id, msg):
|
| 81 |
if user_id not in memory:
|
| 82 |
memory[user_id] = []
|
| 83 |
-
memory[user_id].append(msg)
|
| 84 |
memory[user_id] = memory[user_id][-10:]
|
| 85 |
|
| 86 |
# ======================================================
|
| 87 |
-
# FILE HANDLER
|
| 88 |
# ======================================================
|
| 89 |
|
| 90 |
def process_file(file):
|
|
@@ -93,54 +90,38 @@ def process_file(file):
|
|
| 93 |
|
| 94 |
ext = file.name.split(".")[-1].lower()
|
| 95 |
|
| 96 |
-
# IMAGE
|
| 97 |
if ext in ["png", "jpg", "jpeg", "webp"]:
|
| 98 |
-
img = Image.open(file.name)
|
| 99 |
try:
|
| 100 |
-
|
| 101 |
except:
|
| 102 |
-
|
| 103 |
-
return img, text
|
| 104 |
|
| 105 |
-
#
|
| 106 |
-
if ext
|
| 107 |
try:
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
for page in reader.pages:
|
| 111 |
-
text += page.extract_text() or ""
|
| 112 |
-
return None, text[:3000]
|
| 113 |
except:
|
| 114 |
-
return None, "
|
| 115 |
|
| 116 |
-
|
| 117 |
-
if ext in ["mp3", "wav"]:
|
| 118 |
-
try:
|
| 119 |
-
r = sr.Recognizer()
|
| 120 |
-
with sr.AudioFile(file.name) as source:
|
| 121 |
-
audio = r.record(source)
|
| 122 |
-
text = r.recognize_google(audio)
|
| 123 |
-
return None, text
|
| 124 |
-
except:
|
| 125 |
-
return None, "AUDIO_ERROR"
|
| 126 |
-
|
| 127 |
-
return None, ""
|
| 128 |
|
| 129 |
# ======================================================
|
| 130 |
-
# CHAT
|
| 131 |
# ======================================================
|
| 132 |
|
| 133 |
def chat(user_id, message, file):
|
| 134 |
|
| 135 |
image, file_text = process_file(file)
|
| 136 |
|
| 137 |
-
|
| 138 |
|
| 139 |
prompt = f"""
|
| 140 |
{SYSTEM_PROMPT}
|
| 141 |
|
| 142 |
Memory:
|
| 143 |
-
{
|
| 144 |
|
| 145 |
File:
|
| 146 |
{file_text}
|
|
@@ -170,11 +151,11 @@ demo = gr.Interface(
|
|
| 170 |
inputs=[
|
| 171 |
gr.Textbox(label="User ID"),
|
| 172 |
gr.Textbox(label="Message"),
|
| 173 |
-
gr.File(label="Upload File (image
|
| 174 |
],
|
| 175 |
outputs=gr.Textbox(label="Axel AI Response"),
|
| 176 |
-
title="Axel AI 🚀",
|
| 177 |
-
description="
|
| 178 |
)
|
| 179 |
|
| 180 |
demo.launch()
|
|
|
|
| 1 |
+
python id="fixed1"
|
| 2 |
from transformers import AutoModel, AutoTokenizer
|
| 3 |
import gradio as gr
|
| 4 |
from PIL import Image
|
| 5 |
import time
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# ======================================================
|
| 8 |
# MODEL
|
|
|
|
| 66 |
"""
|
| 67 |
|
| 68 |
# ======================================================
|
| 69 |
+
# MEMORY (SAFE)
|
| 70 |
# ======================================================
|
| 71 |
|
| 72 |
memory = {}
|
| 73 |
|
| 74 |
def get_memory(user_id):
|
| 75 |
+
return "\n".join(memory.get(user_id, [])[-6:])
|
| 76 |
|
| 77 |
def save_memory(user_id, msg):
|
| 78 |
if user_id not in memory:
|
| 79 |
memory[user_id] = []
|
| 80 |
+
memory[user_id].append(str(msg)[:300])
|
| 81 |
memory[user_id] = memory[user_id][-10:]
|
| 82 |
|
| 83 |
# ======================================================
|
| 84 |
+
# FILE HANDLER (SAFE ONLY)
|
| 85 |
# ======================================================
|
| 86 |
|
| 87 |
def process_file(file):
|
|
|
|
| 90 |
|
| 91 |
ext = file.name.split(".")[-1].lower()
|
| 92 |
|
| 93 |
+
# IMAGE ONLY (safe for MiniCPM-V)
|
| 94 |
if ext in ["png", "jpg", "jpeg", "webp"]:
|
|
|
|
| 95 |
try:
|
| 96 |
+
return Image.open(file.name), ""
|
| 97 |
except:
|
| 98 |
+
return None, ""
|
|
|
|
| 99 |
|
| 100 |
+
# TEXT FILES ONLY
|
| 101 |
+
if ext in ["txt", "md"]:
|
| 102 |
try:
|
| 103 |
+
with open(file.name, "r", encoding="utf-8") as f:
|
| 104 |
+
return None, f.read()[:2000]
|
|
|
|
|
|
|
|
|
|
| 105 |
except:
|
| 106 |
+
return None, ""
|
| 107 |
|
| 108 |
+
return None, f"[FILE RECEIVED: {ext}]"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
# ======================================================
|
| 111 |
+
# CHAT
|
| 112 |
# ======================================================
|
| 113 |
|
| 114 |
def chat(user_id, message, file):
|
| 115 |
|
| 116 |
image, file_text = process_file(file)
|
| 117 |
|
| 118 |
+
history = get_memory(user_id)
|
| 119 |
|
| 120 |
prompt = f"""
|
| 121 |
{SYSTEM_PROMPT}
|
| 122 |
|
| 123 |
Memory:
|
| 124 |
+
{history}
|
| 125 |
|
| 126 |
File:
|
| 127 |
{file_text}
|
|
|
|
| 151 |
inputs=[
|
| 152 |
gr.Textbox(label="User ID"),
|
| 153 |
gr.Textbox(label="Message"),
|
| 154 |
+
gr.File(label="Upload File (image or text)")
|
| 155 |
],
|
| 156 |
outputs=gr.Textbox(label="Axel AI Response"),
|
| 157 |
+
title="Axel AI 🚀 (Stable Version)",
|
| 158 |
+
description="Fast multimodal AI for HuggingFace Spaces"
|
| 159 |
)
|
| 160 |
|
| 161 |
demo.launch()
|