Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,87 +1,82 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
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 |
-
|
|
|
15 |
|
16 |
-
def
|
17 |
-
|
18 |
-
|
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 |
-
|
37 |
-
|
38 |
-
return transcript["text"]
|
39 |
except Exception as e:
|
40 |
-
return f"β
|
41 |
|
42 |
-
def
|
43 |
-
|
44 |
-
|
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 |
-
|
|
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
66 |
|
67 |
-
with gr.Blocks(
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
70 |
|
71 |
with gr.Row():
|
72 |
-
|
73 |
-
|
|
|
|
|
74 |
|
75 |
with gr.Row():
|
76 |
-
|
77 |
-
|
78 |
-
upload_img = gr.Image(type="filepath", label="πΌοΈ Upload Image")
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
|
84 |
-
|
85 |
-
|
|
|
|
|
86 |
|
87 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import whisper
|
3 |
import os
|
4 |
+
import uuid
|
5 |
import pdfplumber
|
6 |
import docx
|
7 |
import pandas as pd
|
8 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
# Load Whisper model locally
|
11 |
+
whisper_model = whisper.load_model("base")
|
12 |
|
13 |
+
def transcribe_audio(audio):
|
14 |
+
if audio is None:
|
15 |
+
return "β οΈ No audio file uploaded."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
|
|
17 |
try:
|
18 |
+
result = whisper_model.transcribe(audio)
|
19 |
+
return result["text"]
|
|
|
20 |
except Exception as e:
|
21 |
+
return f"β Whisper error: {str(e)}"
|
22 |
|
23 |
+
def extract_text_from_file(file):
|
24 |
+
if file is None:
|
25 |
+
return "β οΈ No file uploaded."
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
name, ext = os.path.splitext(file.name)
|
28 |
+
ext = ext.lower()
|
29 |
|
30 |
+
try:
|
31 |
+
if ext == ".pdf":
|
32 |
+
with pdfplumber.open(file) as pdf:
|
33 |
+
text = "\n".join(page.extract_text() or "" for page in pdf.pages)
|
34 |
+
elif ext in [".docx"]:
|
35 |
+
doc = docx.Document(file)
|
36 |
+
text = "\n".join(paragraph.text for paragraph in doc.paragraphs)
|
37 |
+
elif ext in [".xlsx", ".xls"]:
|
38 |
+
df = pd.read_excel(file)
|
39 |
+
text = df.to_string(index=False)
|
40 |
+
elif ext in [".png", ".jpg", ".jpeg"]:
|
41 |
+
image = Image.open(file)
|
42 |
+
text = "πΌοΈ Image uploaded. Please describe what you'd like me to do with it."
|
43 |
+
else:
|
44 |
+
text = "β οΈ Unsupported file type."
|
45 |
+
return text
|
46 |
+
except Exception as e:
|
47 |
+
return f"β File processing error: {str(e)}"
|
48 |
|
49 |
+
def chatbot_response(message, chat_history):
|
50 |
+
# Echo-style placeholder response
|
51 |
+
bot_reply = f"π€ You said: {message}"
|
52 |
+
chat_history.append((message, bot_reply))
|
53 |
+
return "", chat_history
|
54 |
|
55 |
+
with gr.Blocks(css="""
|
56 |
+
#chatbox { height: 500px; overflow: auto; }
|
57 |
+
.message-input { height: 40px; border-radius: 6px; }
|
58 |
+
.sidebar { width: 25%; overflow-y: auto; }
|
59 |
+
.main { width: 75%; }
|
60 |
+
""") as demo:
|
61 |
+
gr.Markdown("""<h2 style="text-align:center;">π€ Neobot - Always Listening</h2>""")
|
62 |
|
63 |
with gr.Row():
|
64 |
+
with gr.Column(scale=1, min_width=200):
|
65 |
+
saved_chats = gr.Textbox(label="Saved Chats (auto-listed soon)", interactive=False)
|
66 |
+
with gr.Column(scale=4):
|
67 |
+
chatbot = gr.Chatbot(elem_id="chatbox")
|
68 |
|
69 |
with gr.Row():
|
70 |
+
msg = gr.Textbox(placeholder="Type here or use mic...", elem_classes="message-input")
|
71 |
+
send_btn = gr.Button("Send")
|
|
|
72 |
|
73 |
+
with gr.Row():
|
74 |
+
record_btn = gr.Audio(source="microphone", type="filepath", label="Record Voice")
|
75 |
+
upload_btn = gr.File(label="Upload File", file_types=[".pdf", ".docx", ".xlsx", ".xls", ".jpg", ".jpeg", ".png"])
|
76 |
|
77 |
+
# Event functions
|
78 |
+
send_btn.click(fn=chatbot_response, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
79 |
+
record_btn.change(fn=transcribe_audio, inputs=record_btn, outputs=msg)
|
80 |
+
upload_btn.change(fn=extract_text_from_file, inputs=upload_btn, outputs=msg)
|
81 |
|
82 |
demo.launch()
|