Spaces:
Sleeping
Sleeping
import gradio as gr | |
import os | |
import openai | |
import pdfplumber | |
import docx | |
import pandas as pd | |
from PIL import Image | |
from io import BytesIO | |
import base64 | |
import tempfile | |
import whisper | |
openai.api_key = os.environ.get("GROQ_API_KEY") | |
# Load Whisper model | |
whisper_model = whisper.load_model("base") | |
def extract_text_from_file(file): | |
if file.name.endswith(".pdf"): | |
with pdfplumber.open(file.name) as pdf: | |
text = "\n".join(page.extract_text() for page in pdf.pages if page.extract_text()) | |
elif file.name.endswith(".docx"): | |
doc = docx.Document(file.name) | |
text = "\n".join(p.text for p in doc.paragraphs) | |
elif file.name.endswith(".xlsx"): | |
df = pd.read_excel(file.name) | |
text = df.to_string() | |
elif file.name.endswith((".png", ".jpg", ".jpeg")): | |
img = Image.open(file.name) | |
buffer = BytesIO() | |
img.save(buffer, format="PNG") | |
encoded = base64.b64encode(buffer.getvalue()).decode("utf-8") | |
text = f"[Image uploaded: data:image/png;base64,{encoded[:100]}... (truncated)]" | |
else: | |
with open(file.name, "r", encoding="utf-8", errors="ignore") as f: | |
text = f.read() | |
return text | |
def transcribe_audio(audio_path): | |
result = whisper_model.transcribe(audio_path) | |
return result["text"] | |
def generate_reply(history): | |
messages = [{"role": "system", "content": "You are a helpful assistant."}] | |
for user_msg, bot_msg in history: | |
messages.append({"role": "user", "content": user_msg}) | |
messages.append({"role": "assistant", "content": bot_msg}) | |
response = openai.ChatCompletion.create( | |
model="llama3-8b-8192", | |
messages=messages, | |
temperature=0.7 | |
) | |
reply = response.choices[0].message.content | |
return reply | |
def respond(message, history): | |
reply = generate_reply(history + [[message, ""]]) | |
history.append([message, reply]) | |
return history, "" | |
def handle_file_upload(file, message): | |
if file is None: | |
return message | |
file_content = extract_text_from_file(file) | |
return f"{message}\n\n--- File Content Start ---\n{file_content}\n--- File Content End ---" | |
def handle_audio_upload(audio, message): | |
if audio is None: | |
return message | |
transcription = transcribe_audio(audio) | |
return f"{message}\n\n--- Transcription ---\n{transcription}" | |
with gr.Blocks(css="body { background-color: white; color: black }") as demo: | |
gr.Markdown("<h1 style='text-align: center;'>Neobot</h1>") | |
chatbot = gr.Chatbot(label="Chat", elem_id="chatbox", height=450, type="messages") | |
with gr.Row(): | |
txt = gr.Textbox(placeholder="Type a message or edit transcribed/file content here...", scale=5, show_label=False) | |
send_btn = gr.Button("Send", scale=1) | |
with gr.Row(): | |
upload_btn = gr.File(label="π Upload File", file_types=[".pdf", ".docx", ".txt", ".xlsx", ".png", ".jpg", ".jpeg"]) | |
audio_in = gr.Audio(label="ποΈ Upload Audio", type="filepath") | |
history = gr.State([]) | |
send_btn.click(respond, [txt, history], [chatbot, txt]) | |
upload_btn.change(handle_file_upload, [upload_btn, txt], txt) | |
audio_in.change(handle_audio_upload, [audio_in, txt], txt) | |
demo.launch() | |