JaweriaGenAI commited on
Commit
6d59737
·
verified ·
1 Parent(s): 30b02bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -57
app.py CHANGED
@@ -1,82 +1,108 @@
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()
 
1
  import gradio as gr
2
+ import openai
3
  import os
4
+ import tempfile
5
+ import whisper
6
  import pdfplumber
7
  import docx
8
  import pandas as pd
9
  from PIL import Image
10
 
11
+ # Load Whisper model
12
  whisper_model = whisper.load_model("base")
13
 
14
+ # Set OpenAI API key
15
+ openai.api_key = os.getenv("OPENAI_API_KEY")
 
16
 
17
+ # Global store for chat messages
18
+ chat_history = []
 
 
 
19
 
20
+ # --- File Processor ---
21
  def extract_text_from_file(file):
22
  if file is None:
23
+ return ""
24
+ filename = file.name
25
+ ext = os.path.splitext(filename)[-1].lower()
26
+
27
+ if ext == ".pdf":
28
+ with pdfplumber.open(file) as pdf:
29
+ return "\n".join(page.extract_text() for page in pdf.pages if page.extract_text())
30
+
31
+ elif ext in [".doc", ".docx"]:
32
+ doc = docx.Document(file)
33
+ return "\n".join([para.text for para in doc.paragraphs])
34
 
35
+ elif ext in [".xls", ".xlsx"]:
36
+ df = pd.read_excel(file)
37
+ return df.to_string(index=False)
38
 
39
+ elif ext in [".png", ".jpg", ".jpeg"]:
40
+ image = Image.open(file)
41
+ return "Image uploaded. Please ask a question about the image."
42
+
43
+ else:
44
+ return file.read().decode("utf-8")
45
+
46
+
47
+ # --- Transcription ---
48
+ def transcribe_audio(audio_file):
49
+ if audio_file is None:
50
+ return ""
51
  try:
52
+ result = whisper_model.transcribe(audio_file)
53
+ return result['text']
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  except Exception as e:
55
+ return f"❌ Transcription error: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
+
58
+ # --- Chatbot Logic ---
59
+ def chat_with_gpt(message, uploaded_file):
60
+ file_text = extract_text_from_file(uploaded_file)
61
+
62
+ system_prompt = """
63
+ You are Neobot, an intelligent assistant that can answer questions, transcribe audio, and analyze uploaded documents.
64
+ """
65
+
66
+ user_prompt = message
67
+ if file_text:
68
+ user_prompt = f"This is the content of the uploaded file:\n{file_text}\n\nUser's question: {message}"
69
+
70
+ chat_history.append({"role": "user", "content": user_prompt})
71
+
72
+ try:
73
+ response = openai.ChatCompletion.create(
74
+ model="gpt-4o",
75
+ messages=[{"role": "system", "content": system_prompt}] + chat_history
76
+ )
77
+ reply = response.choices[0].message.content
78
+ chat_history.append({"role": "assistant", "content": reply})
79
+ return [(msg['content'], None) if msg['role'] == 'user' else (None, msg['content']) for msg in chat_history]
80
+ except Exception as e:
81
+ return [[(None, f"❌ Error: {str(e)}")]]
82
+
83
+
84
+ # --- Gradio UI ---
85
+ with gr.Blocks(css="body { background-color: white; color: black; }") as demo:
86
+ gr.Markdown("""<h1 style='text-align: center;'>Neobot</h1>""")
87
+ chatbot = gr.Chatbot(type="messages", elem_id="chatbox")
88
 
89
  with gr.Row():
90
+ msg_box = gr.Textbox(placeholder="Type a message...", show_label=False, scale=8)
91
+ record_btn = gr.Audio(type="filepath", label="Record Voice", show_label=False, scale=2)
92
 
93
  with gr.Row():
94
+ upload_file = gr.File(label="Upload File", file_types=[".pdf", ".docx", ".xlsx", ".txt", ".png", ".jpg"])
95
+
96
+ # --- Callbacks ---
97
+ def handle_voice_input(audio):
98
+ text = transcribe_audio(audio)
99
+ return text
100
+
101
+ def respond(message, file):
102
+ return chat_with_gpt(message, file)
103
 
104
+ record_btn.change(handle_voice_input, inputs=record_btn, outputs=msg_box)
105
+ msg_box.submit(respond, [msg_box, upload_file], chatbot)
 
 
106
 
107
+ # --- Launch ---
108
  demo.launch()