JaweriaGenAI commited on
Commit
f301c1d
Β·
verified Β·
1 Parent(s): 0f9ce1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -12
app.py CHANGED
@@ -3,7 +3,6 @@ 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
 
@@ -32,7 +31,8 @@ def chat_with_groq(message, state):
32
  reply = f"❌ Error: {e}"
33
 
34
  oai_history.append({"role": "assistant", "content": reply})
35
- chatbot_ui.append([message, reply])
 
36
 
37
  return "", chatbot_ui, {"oai_history": oai_history, "chatbot_ui": chatbot_ui}
38
 
@@ -60,12 +60,9 @@ def load_chat(name):
60
  with open(filename, "r", encoding="utf-8") as f:
61
  oai_history = json.load(f)
62
  chatbot_ui = []
63
- for i in range(0, len(oai_history)):
64
- if oai_history[i]["role"] == "user":
65
- user_msg = oai_history[i]["content"]
66
- if i + 1 < len(oai_history) and oai_history[i+1]["role"] == "assistant":
67
- bot_msg = oai_history[i+1]["content"]
68
- chatbot_ui.append([user_msg, bot_msg])
69
  return chatbot_ui, {"oai_history": oai_history, "chatbot_ui": chatbot_ui}, f"βœ… Loaded {name}"
70
  except Exception as e:
71
  return [], {"oai_history": [], "chatbot_ui": []}, f"❌ Could not load {name}: {e}"
@@ -87,14 +84,14 @@ def process_file(file):
87
  if ext == "pdf":
88
  with pdfplumber.open(file.name) as pdf:
89
  text = "\n".join([p.extract_text() for p in pdf.pages if p.extract_text()])
90
- elif ext in ["docx"]:
91
  doc = docx.Document(file.name)
92
  text = "\n".join([p.text for p in doc.paragraphs])
93
  elif ext in ["csv", "xlsx"]:
94
  df = pd.read_csv(file.name) if ext == "csv" else pd.read_excel(file.name)
95
  text = df.to_string()
96
  elif ext in ["png", "jpg", "jpeg"]:
97
- image = Image.open(file.name)
98
  text = "Image uploaded. Please describe what you want to know."
99
  else:
100
  text = file.read().decode("utf-8")
@@ -126,14 +123,14 @@ textarea, input[type='text'] { background: #f0f0f0; border-radius: 8px; }
126
 
127
  gr.Markdown("# πŸ€– Neobot - Always Listening", elem_id="title")
128
 
129
- chatbot = gr.Chatbot()
130
 
131
  with gr.Row():
132
  chat_input = gr.Textbox(placeholder="Type here or use mic...", scale=6, show_label=False)
133
  send_btn = gr.Button("πŸš€", scale=1)
134
 
135
  with gr.Row():
136
- mic_audio = gr.Audio(source="microphone", type="filepath", label="πŸŽ™οΈ Record Voice")
137
  mic_audio.change(transcribe_audio, [mic_audio], [chat_input])
138
 
139
  with gr.Row():
 
3
  import json, re
4
  import openai
5
  from datetime import datetime
 
6
  import pdfplumber, docx, pandas as pd
7
  from PIL import Image
8
 
 
31
  reply = f"❌ Error: {e}"
32
 
33
  oai_history.append({"role": "assistant", "content": reply})
34
+ chatbot_ui.append({"role": "user", "content": message})
35
+ chatbot_ui.append({"role": "assistant", "content": reply})
36
 
37
  return "", chatbot_ui, {"oai_history": oai_history, "chatbot_ui": chatbot_ui}
38
 
 
60
  with open(filename, "r", encoding="utf-8") as f:
61
  oai_history = json.load(f)
62
  chatbot_ui = []
63
+ for m in oai_history:
64
+ if m["role"] in ["user", "assistant"]:
65
+ chatbot_ui.append({"role": m["role"], "content": m["content"]})
 
 
 
66
  return chatbot_ui, {"oai_history": oai_history, "chatbot_ui": chatbot_ui}, f"βœ… Loaded {name}"
67
  except Exception as e:
68
  return [], {"oai_history": [], "chatbot_ui": []}, f"❌ Could not load {name}: {e}"
 
84
  if ext == "pdf":
85
  with pdfplumber.open(file.name) as pdf:
86
  text = "\n".join([p.extract_text() for p in pdf.pages if p.extract_text()])
87
+ elif ext == "docx":
88
  doc = docx.Document(file.name)
89
  text = "\n".join([p.text for p in doc.paragraphs])
90
  elif ext in ["csv", "xlsx"]:
91
  df = pd.read_csv(file.name) if ext == "csv" else pd.read_excel(file.name)
92
  text = df.to_string()
93
  elif ext in ["png", "jpg", "jpeg"]:
94
+ Image.open(file.name) # just validate
95
  text = "Image uploaded. Please describe what you want to know."
96
  else:
97
  text = file.read().decode("utf-8")
 
123
 
124
  gr.Markdown("# πŸ€– Neobot - Always Listening", elem_id="title")
125
 
126
+ chatbot = gr.Chatbot(type="messages")
127
 
128
  with gr.Row():
129
  chat_input = gr.Textbox(placeholder="Type here or use mic...", scale=6, show_label=False)
130
  send_btn = gr.Button("πŸš€", scale=1)
131
 
132
  with gr.Row():
133
+ mic_audio = gr.Audio(type="filepath", label="πŸŽ™οΈ Record Voice")
134
  mic_audio.change(transcribe_audio, [mic_audio], [chat_input])
135
 
136
  with gr.Row():