Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,107 +1,94 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
-
import tempfile
|
4 |
-
import whisper
|
5 |
-
import docx
|
6 |
import pdfplumber
|
|
|
7 |
import pandas as pd
|
8 |
-
from
|
9 |
-
from
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
client =
|
|
|
|
|
13 |
whisper_model = whisper.load_model("base")
|
14 |
|
15 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
if file is None:
|
17 |
-
return
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
return f.read()
|
23 |
-
elif ext == ".pdf":
|
24 |
-
text = ""
|
25 |
-
with pdfplumber.open(file.name) as pdf:
|
26 |
-
for page in pdf.pages:
|
27 |
-
text += page.extract_text() + "\n"
|
28 |
-
return text
|
29 |
-
elif ext in [".doc", ".docx"]:
|
30 |
-
doc = docx.Document(file.name)
|
31 |
-
return "\n".join([para.text for para in doc.paragraphs])
|
32 |
-
elif ext in [".xls", ".xlsx"]:
|
33 |
-
df = pd.read_excel(file.name)
|
34 |
-
return df.to_string(index=False)
|
35 |
-
else:
|
36 |
-
return f"[Unsupported file format: {ext}]"
|
37 |
-
except Exception as e:
|
38 |
-
return f"[Error reading file: {e}]"
|
39 |
-
|
40 |
-
def transcribe_audio(audio):
|
41 |
if audio is None:
|
42 |
-
return
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
def process_input(message, history):
|
51 |
-
if not message.strip():
|
52 |
-
return history
|
53 |
-
|
54 |
-
history = history or []
|
55 |
-
history.append({"role": "user", "content": message})
|
56 |
-
|
57 |
-
try:
|
58 |
-
response = client.chat.completions.create(
|
59 |
-
model="llama3-8b-8192",
|
60 |
-
messages=history,
|
61 |
-
)
|
62 |
-
reply = response.choices[0].message.content
|
63 |
-
history.append({"role": "assistant", "content": reply})
|
64 |
-
except Exception as e:
|
65 |
-
history.append({"role": "assistant", "content": f"[Error: {e}]"})
|
66 |
-
|
67 |
-
return history
|
68 |
-
|
69 |
-
def interface_func(message, history, file, audio):
|
70 |
-
file_text = read_file(file) if file else ""
|
71 |
-
audio_text = transcribe_audio(audio) if audio else ""
|
72 |
-
combined = ""
|
73 |
-
|
74 |
-
if file_text:
|
75 |
-
combined += f"{file.name} content:\n{file_text}\n\n"
|
76 |
-
if audio_text:
|
77 |
-
combined += f"Transcribed audio:\n{audio_text}\n\n"
|
78 |
-
if message:
|
79 |
-
combined += message
|
80 |
-
|
81 |
-
return combined.strip()
|
82 |
-
|
83 |
-
with gr.Blocks(css="textarea { font-size: 16px !important; }") as demo:
|
84 |
-
gr.Markdown("<h1 style='text-align: center;'>NEOBOT</h1>")
|
85 |
-
|
86 |
-
chatbot = gr.Chatbot(height=400, label="Chat")
|
87 |
|
88 |
with gr.Row():
|
89 |
-
|
90 |
-
scale=8,
|
91 |
-
placeholder="Ask something...",
|
92 |
-
show_label=False,
|
93 |
-
container=True
|
94 |
-
)
|
95 |
send_btn = gr.Button("Send", scale=1)
|
96 |
|
97 |
with gr.Row():
|
98 |
-
|
99 |
-
|
|
|
|
|
100 |
|
101 |
-
|
|
|
102 |
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
|
107 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
|
|
|
|
|
|
3 |
import pdfplumber
|
4 |
+
import docx
|
5 |
import pandas as pd
|
6 |
+
from PIL import Image
|
7 |
+
from io import BytesIO
|
8 |
+
import base64
|
9 |
+
import whisper
|
10 |
+
from openai import OpenAI
|
11 |
|
12 |
+
# Setup GROQ
|
13 |
+
client = OpenAI(api_key=os.environ["GROQ_API_KEY"], base_url="https://api.groq.com/openai/v1")
|
14 |
+
|
15 |
+
# Load Whisper model
|
16 |
whisper_model = whisper.load_model("base")
|
17 |
|
18 |
+
def extract_text_from_file(file):
|
19 |
+
if file.name.endswith(".pdf"):
|
20 |
+
with pdfplumber.open(file.name) as pdf:
|
21 |
+
text = "\n".join(page.extract_text() for page in pdf.pages if page.extract_text())
|
22 |
+
elif file.name.endswith(".docx"):
|
23 |
+
doc = docx.Document(file.name)
|
24 |
+
text = "\n".join(p.text for p in doc.paragraphs)
|
25 |
+
elif file.name.endswith(".xlsx"):
|
26 |
+
df = pd.read_excel(file.name)
|
27 |
+
text = df.to_string()
|
28 |
+
elif file.name.endswith((".png", ".jpg", ".jpeg")):
|
29 |
+
img = Image.open(file.name)
|
30 |
+
buffer = BytesIO()
|
31 |
+
img.save(buffer, format="PNG")
|
32 |
+
encoded = base64.b64encode(buffer.getvalue()).decode("utf-8")
|
33 |
+
text = f"[Image uploaded: data:image/png;base64,{encoded[:100]}... (truncated)]"
|
34 |
+
else:
|
35 |
+
with open(file.name, "r", encoding="utf-8", errors="ignore") as f:
|
36 |
+
text = f.read()
|
37 |
+
return text
|
38 |
+
|
39 |
+
def transcribe_audio(audio_path):
|
40 |
+
result = whisper_model.transcribe(audio_path)
|
41 |
+
return result["text"]
|
42 |
+
|
43 |
+
def generate_reply(history):
|
44 |
+
messages = [{"role": "system", "content": "You are a helpful assistant."}]
|
45 |
+
for user_msg, bot_msg in history:
|
46 |
+
messages.append({"role": "user", "content": user_msg})
|
47 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
48 |
+
|
49 |
+
response = client.chat.completions.create(
|
50 |
+
model="llama3-8b-8192",
|
51 |
+
messages=messages,
|
52 |
+
temperature=0.7
|
53 |
+
)
|
54 |
+
return response.choices[0].message.content
|
55 |
+
|
56 |
+
def respond(message, history):
|
57 |
+
reply = generate_reply(history + [[message, ""]])
|
58 |
+
history.append([message, reply])
|
59 |
+
return history, ""
|
60 |
+
|
61 |
+
def handle_file_upload(file, message):
|
62 |
if file is None:
|
63 |
+
return message
|
64 |
+
file_content = extract_text_from_file(file)
|
65 |
+
return f"{message}\n\n--- File Content Start ---\n{file_content}\n--- File Content End ---"
|
66 |
+
|
67 |
+
def handle_audio_upload(audio, message):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
if audio is None:
|
69 |
+
return message
|
70 |
+
transcription = transcribe_audio(audio)
|
71 |
+
return f"{message}\n\n--- Transcription ---\n{transcription}"
|
72 |
+
|
73 |
+
with gr.Blocks(css="body { background-color: white; color: black }") as demo:
|
74 |
+
gr.Markdown("<h1 style='text-align: center;'>Neobot</h1>")
|
75 |
+
chatbot = gr.Chatbot(label="Chat", elem_id="chatbox", height=450, type="messages")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
with gr.Row():
|
78 |
+
txt = gr.Textbox(placeholder="Type your message or review the file/audio content here…", scale=5, show_label=False)
|
|
|
|
|
|
|
|
|
|
|
79 |
send_btn = gr.Button("Send", scale=1)
|
80 |
|
81 |
with gr.Row():
|
82 |
+
upload_btn = gr.File(label="📎 Upload File", file_types=[".pdf", ".docx", ".txt", ".xlsx", ".png", ".jpg", ".jpeg"])
|
83 |
+
audio_in = gr.Audio(label="🎙️ Upload Audio", type="filepath")
|
84 |
+
|
85 |
+
history = gr.State([])
|
86 |
|
87 |
+
# Message only sent when Send is clicked
|
88 |
+
send_btn.click(respond, [txt, history], [chatbot, txt])
|
89 |
|
90 |
+
# File and audio just modify the message box
|
91 |
+
upload_btn.change(handle_file_upload, [upload_btn, txt], txt)
|
92 |
+
audio_in.change(handle_audio_upload, [audio_in, txt], txt)
|
93 |
|
94 |
demo.launch()
|