Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,84 +1,92 @@
|
|
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
|
|
|
9 |
import tempfile
|
|
|
10 |
|
11 |
-
|
12 |
-
whisper_model = whisper.load_model("base")
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
|
17 |
-
# Helper functions
|
18 |
def extract_text_from_file(file):
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
else:
|
35 |
-
|
|
|
|
|
36 |
|
37 |
def transcribe_audio(audio_path):
|
38 |
-
|
39 |
-
|
40 |
-
return result["text"]
|
41 |
-
except Exception as e:
|
42 |
-
return f"Transcription error: {str(e)}"
|
43 |
|
44 |
-
def
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
)
|
50 |
-
return response.choices[0].message.content.strip()
|
51 |
-
except Exception as e:
|
52 |
-
return f"β Error: {str(e)}"
|
53 |
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
|
75 |
-
|
76 |
-
user_input = gr.Textbox(placeholder="Type a message...", label=None)
|
77 |
-
file_upload = gr.File(label="π Upload File", file_types=[".pdf", ".docx", ".csv", ".xlsx", ".jpg", ".jpeg", ".png"])
|
78 |
-
audio_input = gr.Audio(type="filepath", label="π€ Record Voice")
|
79 |
-
submit_btn = gr.Button("Send")
|
80 |
|
81 |
-
|
|
|
|
|
82 |
|
83 |
-
|
84 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import os
|
3 |
+
import openai
|
4 |
import pdfplumber
|
5 |
import docx
|
6 |
import pandas as pd
|
7 |
from PIL import Image
|
8 |
+
from io import BytesIO
|
9 |
+
import base64
|
10 |
import tempfile
|
11 |
+
import whisper
|
12 |
|
13 |
+
openai.api_key = os.environ.get("GROQ_API_KEY")
|
|
|
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 = openai.ChatCompletion.create(
|
50 |
+
model="llama3-8b-8192",
|
51 |
+
messages=messages,
|
52 |
+
temperature=0.7
|
53 |
+
)
|
54 |
+
reply = response.choices[0].message.content
|
55 |
+
return reply
|
56 |
|
57 |
+
def respond(message, history):
|
58 |
+
reply = generate_reply(history + [[message, ""]])
|
59 |
+
history.append([message, reply])
|
60 |
+
return history, ""
|
61 |
+
|
62 |
+
def handle_file_upload(file, message):
|
63 |
+
if file is None:
|
64 |
+
return message
|
65 |
+
file_content = extract_text_from_file(file)
|
66 |
+
return f"{message}\n\n--- File Content Start ---\n{file_content}\n--- File Content End ---"
|
67 |
+
|
68 |
+
def handle_audio_upload(audio, message):
|
69 |
+
if audio is None:
|
70 |
+
return message
|
71 |
+
transcription = transcribe_audio(audio)
|
72 |
+
return f"{message}\n\n--- Transcription ---\n{transcription}"
|
73 |
+
|
74 |
+
with gr.Blocks(css="body { background-color: white; color: black }") as demo:
|
75 |
+
gr.Markdown("<h1 style='text-align: center;'>Neobot</h1>")
|
76 |
+
chatbot = gr.Chatbot(label="Chat", elem_id="chatbox", height=450, type="messages")
|
77 |
|
78 |
+
with gr.Row():
|
79 |
+
txt = gr.Textbox(placeholder="Type a message or edit transcribed/file content here...", scale=5, show_label=False)
|
80 |
+
send_btn = gr.Button("Send", scale=1)
|
81 |
|
82 |
+
with gr.Row():
|
83 |
+
upload_btn = gr.File(label="π Upload File", file_types=[".pdf", ".docx", ".txt", ".xlsx", ".png", ".jpg", ".jpeg"])
|
84 |
+
audio_in = gr.Audio(label="ποΈ Upload Audio", type="filepath")
|
85 |
|
86 |
+
history = gr.State([])
|
|
|
|
|
|
|
|
|
87 |
|
88 |
+
send_btn.click(respond, [txt, history], [chatbot, txt])
|
89 |
+
upload_btn.change(handle_file_upload, [upload_btn, txt], txt)
|
90 |
+
audio_in.change(handle_audio_upload, [audio_in, txt], txt)
|
91 |
|
92 |
+
demo.launch()
|
|