Spaces:
Sleeping
Sleeping
File size: 2,964 Bytes
aa9cd89 b469735 0fb9914 2ddc050 0fb9914 333d381 0fb9914 333d381 0fb9914 333d381 0fb9914 2ddc050 0fb9914 2ddc050 0fb9914 2ddc050 0fb9914 e841288 0fb9914 b469735 0fb9914 b469735 0fb9914 b469735 0fb9914 2ddc050 0815af0 0fb9914 b469735 0fb9914 b469735 0fb9914 3b96490 e491ba1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
import gradio as gr
import openai
import os
import pdfplumber
import docx
import pandas as pd
from PIL import Image
import pytesseract
from pydub import AudioSegment
import tempfile
import sounddevice as sd
import scipy.io.wavfile as wav
openai.api_key = os.getenv("OPENAI_API_KEY")
def extract_text(file):
ext = file.name.split(".")[-1].lower()
if ext == "pdf":
with pdfplumber.open(file.name) as pdf:
return "\n".join([page.extract_text() for page in pdf.pages if page.extract_text()])
elif ext in ["doc", "docx"]:
doc = docx.Document(file.name)
return "\n".join([p.text for p in doc.paragraphs])
elif ext in ["xls", "xlsx", "csv"]:
df = pd.read_excel(file.name) if ext != "csv" else pd.read_csv(file.name)
return df.to_string()
elif ext in ["jpg", "jpeg", "png"]:
image = Image.open(file.name)
text = pytesseract.image_to_string(image)
return text or "β No text found in image."
else:
return "β Unsupported file format."
def transcribe_audio(audio_path):
try:
with open(audio_path, "rb") as f:
transcript = openai.Audio.transcribe("whisper-1", f)
return transcript["text"]
except Exception as e:
return f"β Transcription error: {str(e)}"
def generate_response(messages):
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages
)
return response.choices[0].message["content"]
except Exception as e:
return f"β Error: {str(e)}"
chat_history = []
def chat(user_message, file=None, image=None, mic_audio=None):
if mic_audio:
user_message = transcribe_audio(mic_audio)
elif file:
user_message = extract_text(file)
elif image:
user_message = extract_text(image)
chat_history.append({"role": "user", "content": user_message})
bot_response = generate_response(chat_history)
chat_history.append({"role": "assistant", "content": bot_response})
return bot_response
with gr.Blocks() as demo:
gr.Markdown("### π§ **Neobot - Always Listening**")
chatbot = gr.Chatbot(height=300)
with gr.Row():
txt = gr.Textbox(placeholder="Type here or use mic...", scale=4)
send_btn = gr.Button("π", scale=1)
with gr.Row():
mic_audio = gr.Audio(type="filepath", label="π€ Record Voice", interactive=True)
upload_file = gr.File(label="π Upload File")
upload_img = gr.Image(type="filepath", label="πΌοΈ Upload Image")
def handle_input(message, file, image, mic_audio):
reply = chat(message, file, image, mic_audio)
return chatbot.update(chatbot.value + [[message, reply]])
send_btn.click(handle_input, inputs=[txt, upload_file, upload_img, mic_audio], outputs=chatbot)
txt.submit(handle_input, inputs=[txt, upload_file, upload_img, mic_audio], outputs=chatbot)
demo.launch()
|