JaweriaGenAI commited on
Commit
0fb9914
Β·
verified Β·
1 Parent(s): 2997eb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -139
app.py CHANGED
@@ -1,160 +1,87 @@
1
- import os
2
  import gradio as gr
3
- import json, re
4
  import openai
5
- from datetime import datetime
6
- from pydub import AudioSegment
7
- import pdfplumber, docx, pandas as pd
 
8
  from PIL import Image
9
- import whisper # Local whisper for transcription
10
-
11
- openai.api_key = os.environ.get("GROQ_API_KEY")
12
- openai.api_base = "https://api.groq.com/openai/v1"
13
-
14
- model_whisper = whisper.load_model("base")
15
-
16
- # Chat with Groq
17
- def chat_with_groq(message, state):
18
- if state is None:
19
- state = {"oai_history": [], "chatbot_ui": []}
20
-
21
- oai_history = state["oai_history"]
22
- chatbot_ui = state["chatbot_ui"]
23
-
24
- if not oai_history:
25
- oai_history.append({"role": "system", "content": "You are Neobot πŸ€– – helpful and concise."})
26
-
27
- oai_history.append({"role": "user", "content": message})
28
- try:
29
- response = openai.ChatCompletion.create(
30
- model="llama3-70b-8192",
31
- messages=oai_history
32
- )
33
- reply = response["choices"][0]["message"]["content"]
34
- except Exception as e:
35
- reply = f"❌ Error: {e}"
36
-
37
- oai_history.append({"role": "assistant", "content": reply})
38
- chatbot_ui.append({"role": "user", "content": message})
39
- chatbot_ui.append({"role": "assistant", "content": reply})
40
-
41
- return "", chatbot_ui, {"oai_history": oai_history, "chatbot_ui": chatbot_ui}
42
-
43
- # Save chat
44
- def save_session(state):
45
- if not state or not state.get("oai_history"):
46
- return "❌ No chat to save"
47
-
48
- oai_history = state["oai_history"]
49
- prompt = next((m["content"] for m in oai_history if m["role"] == "user"), "chat")
50
- title = re.sub(r"[^\w\s]", "", prompt).strip()
51
- title = " ".join(title.split()[:6]) or "Chat"
52
- timestamp = datetime.now().strftime("%b %d %Y %H-%M")
53
- filename = f"{title} - {timestamp}.json"
54
- with open(filename, "w", encoding="utf-8") as f:
55
- json.dump(oai_history, f, indent=2, ensure_ascii=False)
56
- return f"βœ… Chat saved as: {filename[:-5]}"
57
-
58
- def list_saved_files():
59
- return sorted([f[:-5] for f in os.listdir() if f.endswith(".json")])
60
-
61
- def load_chat(name):
62
- filename = f"{name}.json"
63
- try:
64
- with open(filename, "r", encoding="utf-8") as f:
65
- oai_history = json.load(f)
66
- chatbot_ui = []
67
- for i in range(0, len(oai_history)):
68
- if oai_history[i]["role"] == "user":
69
- chatbot_ui.append({"role": "user", "content": oai_history[i]["content"]})
70
- elif oai_history[i]["role"] == "assistant":
71
- chatbot_ui.append({"role": "assistant", "content": oai_history[i]["content"]})
72
- return chatbot_ui, {"oai_history": oai_history, "chatbot_ui": chatbot_ui}, f"βœ… Loaded {name}"
73
- except Exception as e:
74
- return [], {"oai_history": [], "chatbot_ui": []}, f"❌ Could not load {name}: {e}"
75
-
76
- # Transcription using local Whisper
77
- def transcribe_audio(file):
78
- if not file:
79
- return ""
80
  try:
81
- audio = whisper.load_audio(file)
82
- audio = whisper.pad_or_trim(audio)
83
- mel = whisper.log_mel_spectrogram(audio).to(model_whisper.device)
84
- result = model_whisper.decode(mel)
85
- return result.text
86
  except Exception as e:
87
- return f"❌ Transcription error: {e}"
88
 
89
- # File processor – send file content to textbox for user to ask about
90
- def file_to_message(file, state):
91
- if file is None:
92
- return "", state
93
- filename = file.name
94
- ext = filename.split(".")[-1].lower()
95
  try:
96
- if ext == "pdf":
97
- with pdfplumber.open(file.name) as pdf:
98
- text = "\n".join([p.extract_text() for p in pdf.pages if p.extract_text()])
99
- elif ext == "docx":
100
- doc = docx.Document(file.name)
101
- text = "\n".join([p.text for p in doc.paragraphs])
102
- elif ext in ["csv", "xlsx"]:
103
- df = pd.read_csv(file.name) if ext == "csv" else pd.read_excel(file.name)
104
- text = df.to_string()
105
- elif ext in ["png", "jpg", "jpeg"]:
106
- text = "[Image uploaded: please describe what you'd like me to do with it.]"
107
- else:
108
- text = file.read().decode("utf-8")
109
-
110
- if len(text) > 8000:
111
- text = text[:8000]
112
-
113
- return text, state
114
  except Exception as e:
115
- return f"❌ Error processing file: {e}", state
116
 
117
- # UI
118
- with gr.Blocks(css="""
119
- body { background: #fff; color: #000; }
120
- #title { font-size: 2rem; text-align: center; margin-top: 1rem; }
121
- .gr-chatbot { height: 70vh !important; }
122
- textarea, input[type='text'] { background: #f0f0f0; border-radius: 8px; }
123
- .gr-button { border-radius: 8px; background: #000; color: white; }
124
- """) as demo:
125
 
126
- state = gr.State({"oai_history": [], "chatbot_ui": []})
 
 
 
 
 
 
127
 
128
- gr.Markdown("# πŸ€– Neobot - Always Listening", elem_id="title")
 
 
 
129
 
130
- chatbot = gr.Chatbot(type="messages")
 
 
131
 
132
  with gr.Row():
133
- chat_input = gr.Textbox(placeholder="Type here or use mic...", scale=6, show_label=False)
134
  send_btn = gr.Button("πŸš€", scale=1)
135
 
136
  with gr.Row():
137
- mic_audio = gr.Audio(type="filepath", label="πŸŽ™οΈ Record Voice", interactive=True)
138
- mic_audio.change(transcribe_audio, [mic_audio], [chat_input])
139
-
140
- with gr.Row():
141
- file_upload = gr.File(label="πŸ“Ž Upload file", file_types=[".pdf", ".docx", ".txt", ".csv", ".xlsx", ".jpg", ".png"])
142
- file_upload.change(file_to_message, [file_upload, state], [chat_input, state])
143
-
144
- with gr.Row():
145
- new_btn = gr.Button("πŸ†• New Chat")
146
- save_btn = gr.Button("πŸ’Ύ Save Chat")
147
- dropdown = gr.Dropdown(label="πŸ“‚ Saved Chats", choices=list_saved_files(), interactive=True)
148
- load_btn = gr.Button("πŸ“₯ Load Chat")
149
-
150
- status = gr.Markdown()
151
 
152
- send_btn.click(chat_with_groq, [chat_input, state], [chat_input, chatbot, state])
153
- chat_input.submit(chat_with_groq, [chat_input, state], [chat_input, chatbot, state])
 
154
 
155
- new_btn.click(lambda: ("", [], {"oai_history": [], "chatbot_ui": []}), [], [chat_input, chatbot, state])
156
- save_btn.click(save_session, [state], [status])
157
- save_btn.click(lambda: gr.update(choices=list_saved_files()), [], [dropdown])
158
- load_btn.click(load_chat, [dropdown], [chatbot, state, status])
159
 
160
  demo.launch()
 
 
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()