JaweriaGenAI commited on
Commit
bad737e
Β·
verified Β·
1 Parent(s): 54a7fc2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -63
app.py CHANGED
@@ -1,87 +1,82 @@
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 pytesseract
9
- from pydub import AudioSegment
10
- import tempfile
11
- import sounddevice as sd
12
- import scipy.io.wavfile as wav
13
 
14
- openai.api_key = os.getenv("OPENAI_API_KEY")
 
15
 
16
- def extract_text(file):
17
- ext = file.name.split(".")[-1].lower()
18
- if ext == "pdf":
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
- with open(audio_path, "rb") as f:
37
- transcript = openai.Audio.transcribe("whisper-1", f)
38
- return transcript["text"]
39
  except Exception as e:
40
- return f"❌ Transcription error: {str(e)}"
41
 
42
- def generate_response(messages):
43
- try:
44
- response = openai.ChatCompletion.create(
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
- chat_history = []
 
53
 
54
- def chat(user_message, file=None, image=None, mic_audio=None):
55
- if mic_audio:
56
- user_message = transcribe_audio(mic_audio)
57
- elif file:
58
- user_message = extract_text(file)
59
- elif image:
60
- user_message = extract_text(image)
 
 
 
 
 
 
 
 
 
 
 
61
 
62
- chat_history.append({"role": "user", "content": user_message})
63
- bot_response = generate_response(chat_history)
64
- chat_history.append({"role": "assistant", "content": bot_response})
65
- return bot_response
 
66
 
67
- with gr.Blocks() as demo:
68
- gr.Markdown("### 🎧 **Neobot - Always Listening**")
69
- chatbot = gr.Chatbot(height=300)
 
 
 
 
70
 
71
  with gr.Row():
72
- txt = gr.Textbox(placeholder="Type here or use mic...", scale=4)
73
- send_btn = gr.Button("πŸš€", scale=1)
 
 
74
 
75
  with gr.Row():
76
- mic_audio = gr.Audio(type="filepath", label="🎀 Record Voice", interactive=True)
77
- upload_file = gr.File(label="πŸ“Ž Upload File")
78
- upload_img = gr.Image(type="filepath", label="πŸ–ΌοΈ Upload Image")
79
 
80
- def handle_input(message, file, image, mic_audio):
81
- reply = chat(message, file, image, mic_audio)
82
- return chatbot.update(chatbot.value + [[message, reply]])
83
 
84
- send_btn.click(handle_input, inputs=[txt, upload_file, upload_img, mic_audio], outputs=chatbot)
85
- txt.submit(handle_input, inputs=[txt, upload_file, upload_img, mic_audio], outputs=chatbot)
 
 
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()