Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,160 +1,87 @@
|
|
1 |
-
import os
|
2 |
import gradio as gr
|
3 |
-
import json, re
|
4 |
import openai
|
5 |
-
|
6 |
-
|
7 |
-
import
|
|
|
8 |
from PIL import Image
|
9 |
-
import
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
def
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
oai_history.append({"role": "assistant", "content": reply})
|
38 |
-
chatbot_ui.append({"role": "user", "content": message})
|
39 |
-
chatbot_ui.append({"role": "assistant", "content": reply})
|
40 |
-
|
41 |
-
return "", chatbot_ui, {"oai_history": oai_history, "chatbot_ui": chatbot_ui}
|
42 |
-
|
43 |
-
# Save chat
|
44 |
-
def save_session(state):
|
45 |
-
if not state or not state.get("oai_history"):
|
46 |
-
return "β No chat to save"
|
47 |
-
|
48 |
-
oai_history = state["oai_history"]
|
49 |
-
prompt = next((m["content"] for m in oai_history if m["role"] == "user"), "chat")
|
50 |
-
title = re.sub(r"[^\w\s]", "", prompt).strip()
|
51 |
-
title = " ".join(title.split()[:6]) or "Chat"
|
52 |
-
timestamp = datetime.now().strftime("%b %d %Y %H-%M")
|
53 |
-
filename = f"{title} - {timestamp}.json"
|
54 |
-
with open(filename, "w", encoding="utf-8") as f:
|
55 |
-
json.dump(oai_history, f, indent=2, ensure_ascii=False)
|
56 |
-
return f"β
Chat saved as: {filename[:-5]}"
|
57 |
-
|
58 |
-
def list_saved_files():
|
59 |
-
return sorted([f[:-5] for f in os.listdir() if f.endswith(".json")])
|
60 |
-
|
61 |
-
def load_chat(name):
|
62 |
-
filename = f"{name}.json"
|
63 |
-
try:
|
64 |
-
with open(filename, "r", encoding="utf-8") as f:
|
65 |
-
oai_history = json.load(f)
|
66 |
-
chatbot_ui = []
|
67 |
-
for i in range(0, len(oai_history)):
|
68 |
-
if oai_history[i]["role"] == "user":
|
69 |
-
chatbot_ui.append({"role": "user", "content": oai_history[i]["content"]})
|
70 |
-
elif oai_history[i]["role"] == "assistant":
|
71 |
-
chatbot_ui.append({"role": "assistant", "content": oai_history[i]["content"]})
|
72 |
-
return chatbot_ui, {"oai_history": oai_history, "chatbot_ui": chatbot_ui}, f"β
Loaded {name}"
|
73 |
-
except Exception as e:
|
74 |
-
return [], {"oai_history": [], "chatbot_ui": []}, f"β Could not load {name}: {e}"
|
75 |
-
|
76 |
-
# Transcription using local Whisper
|
77 |
-
def transcribe_audio(file):
|
78 |
-
if not file:
|
79 |
-
return ""
|
80 |
try:
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
result = model_whisper.decode(mel)
|
85 |
-
return result.text
|
86 |
except Exception as e:
|
87 |
-
return f"β Transcription error: {e}"
|
88 |
|
89 |
-
|
90 |
-
def file_to_message(file, state):
|
91 |
-
if file is None:
|
92 |
-
return "", state
|
93 |
-
filename = file.name
|
94 |
-
ext = filename.split(".")[-1].lower()
|
95 |
try:
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
text = "\n".join([p.text for p in doc.paragraphs])
|
102 |
-
elif ext in ["csv", "xlsx"]:
|
103 |
-
df = pd.read_csv(file.name) if ext == "csv" else pd.read_excel(file.name)
|
104 |
-
text = df.to_string()
|
105 |
-
elif ext in ["png", "jpg", "jpeg"]:
|
106 |
-
text = "[Image uploaded: please describe what you'd like me to do with it.]"
|
107 |
-
else:
|
108 |
-
text = file.read().decode("utf-8")
|
109 |
-
|
110 |
-
if len(text) > 8000:
|
111 |
-
text = text[:8000]
|
112 |
-
|
113 |
-
return text, state
|
114 |
except Exception as e:
|
115 |
-
return f"β Error
|
116 |
|
117 |
-
|
118 |
-
with gr.Blocks(css="""
|
119 |
-
body { background: #fff; color: #000; }
|
120 |
-
#title { font-size: 2rem; text-align: center; margin-top: 1rem; }
|
121 |
-
.gr-chatbot { height: 70vh !important; }
|
122 |
-
textarea, input[type='text'] { background: #f0f0f0; border-radius: 8px; }
|
123 |
-
.gr-button { border-radius: 8px; background: #000; color: white; }
|
124 |
-
""") as demo:
|
125 |
|
126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
|
128 |
-
|
|
|
|
|
|
|
129 |
|
130 |
-
|
|
|
|
|
131 |
|
132 |
with gr.Row():
|
133 |
-
|
134 |
send_btn = gr.Button("π", scale=1)
|
135 |
|
136 |
with gr.Row():
|
137 |
-
mic_audio = gr.Audio(type="filepath", label="
|
138 |
-
|
139 |
-
|
140 |
-
with gr.Row():
|
141 |
-
file_upload = gr.File(label="π Upload file", file_types=[".pdf", ".docx", ".txt", ".csv", ".xlsx", ".jpg", ".png"])
|
142 |
-
file_upload.change(file_to_message, [file_upload, state], [chat_input, state])
|
143 |
-
|
144 |
-
with gr.Row():
|
145 |
-
new_btn = gr.Button("π New Chat")
|
146 |
-
save_btn = gr.Button("πΎ Save Chat")
|
147 |
-
dropdown = gr.Dropdown(label="π Saved Chats", choices=list_saved_files(), interactive=True)
|
148 |
-
load_btn = gr.Button("π₯ Load Chat")
|
149 |
-
|
150 |
-
status = gr.Markdown()
|
151 |
|
152 |
-
|
153 |
-
|
|
|
154 |
|
155 |
-
|
156 |
-
|
157 |
-
save_btn.click(lambda: gr.update(choices=list_saved_files()), [], [dropdown])
|
158 |
-
load_btn.click(load_chat, [dropdown], [chatbot, state, status])
|
159 |
|
160 |
demo.launch()
|
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import openai
|
3 |
+
import os
|
4 |
+
import pdfplumber
|
5 |
+
import docx
|
6 |
+
import pandas as pd
|
7 |
from PIL import Image
|
8 |
+
import pytesseract
|
9 |
+
from pydub import AudioSegment
|
10 |
+
import tempfile
|
11 |
+
import sounddevice as sd
|
12 |
+
import scipy.io.wavfile as wav
|
13 |
+
|
14 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
15 |
+
|
16 |
+
def extract_text(file):
|
17 |
+
ext = file.name.split(".")[-1].lower()
|
18 |
+
if ext == "pdf":
|
19 |
+
with pdfplumber.open(file.name) as pdf:
|
20 |
+
return "\n".join([page.extract_text() for page in pdf.pages if page.extract_text()])
|
21 |
+
elif ext in ["doc", "docx"]:
|
22 |
+
doc = docx.Document(file.name)
|
23 |
+
return "\n".join([p.text for p in doc.paragraphs])
|
24 |
+
elif ext in ["xls", "xlsx", "csv"]:
|
25 |
+
df = pd.read_excel(file.name) if ext != "csv" else pd.read_csv(file.name)
|
26 |
+
return df.to_string()
|
27 |
+
elif ext in ["jpg", "jpeg", "png"]:
|
28 |
+
image = Image.open(file.name)
|
29 |
+
text = pytesseract.image_to_string(image)
|
30 |
+
return text or "β No text found in image."
|
31 |
+
else:
|
32 |
+
return "β Unsupported file format."
|
33 |
+
|
34 |
+
def transcribe_audio(audio_path):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
try:
|
36 |
+
with open(audio_path, "rb") as f:
|
37 |
+
transcript = openai.Audio.transcribe("whisper-1", f)
|
38 |
+
return transcript["text"]
|
|
|
|
|
39 |
except Exception as e:
|
40 |
+
return f"β Transcription error: {str(e)}"
|
41 |
|
42 |
+
def generate_response(messages):
|
|
|
|
|
|
|
|
|
|
|
43 |
try:
|
44 |
+
response = openai.ChatCompletion.create(
|
45 |
+
model="gpt-3.5-turbo",
|
46 |
+
messages=messages
|
47 |
+
)
|
48 |
+
return response.choices[0].message["content"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
except Exception as e:
|
50 |
+
return f"β Error: {str(e)}"
|
51 |
|
52 |
+
chat_history = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
+
def chat(user_message, file=None, image=None, mic_audio=None):
|
55 |
+
if mic_audio:
|
56 |
+
user_message = transcribe_audio(mic_audio)
|
57 |
+
elif file:
|
58 |
+
user_message = extract_text(file)
|
59 |
+
elif image:
|
60 |
+
user_message = extract_text(image)
|
61 |
|
62 |
+
chat_history.append({"role": "user", "content": user_message})
|
63 |
+
bot_response = generate_response(chat_history)
|
64 |
+
chat_history.append({"role": "assistant", "content": bot_response})
|
65 |
+
return bot_response
|
66 |
|
67 |
+
with gr.Blocks() as demo:
|
68 |
+
gr.Markdown("### π§ **Neobot - Always Listening**")
|
69 |
+
chatbot = gr.Chatbot(height=300)
|
70 |
|
71 |
with gr.Row():
|
72 |
+
txt = gr.Textbox(placeholder="Type here or use mic...", scale=4)
|
73 |
send_btn = gr.Button("π", scale=1)
|
74 |
|
75 |
with gr.Row():
|
76 |
+
mic_audio = gr.Audio(type="filepath", label="π€ Record Voice", interactive=True)
|
77 |
+
upload_file = gr.File(label="π Upload File")
|
78 |
+
upload_img = gr.Image(type="filepath", label="πΌοΈ Upload Image")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
+
def handle_input(message, file, image, mic_audio):
|
81 |
+
reply = chat(message, file, image, mic_audio)
|
82 |
+
return chatbot.update(chatbot.value + [[message, reply]])
|
83 |
|
84 |
+
send_btn.click(handle_input, inputs=[txt, upload_file, upload_img, mic_audio], outputs=chatbot)
|
85 |
+
txt.submit(handle_input, inputs=[txt, upload_file, upload_img, mic_audio], outputs=chatbot)
|
|
|
|
|
86 |
|
87 |
demo.launch()
|