JaweriaGenAI commited on
Commit
637e644
Β·
verified Β·
1 Parent(s): 9d798d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -62
app.py CHANGED
@@ -1,84 +1,92 @@
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 whisper
 
9
  import tempfile
 
10
 
11
- # Load Whisper model for transcription
12
- whisper_model = whisper.load_model("base")
13
 
14
- # Set your OpenAI API key
15
- openai.api_key = os.getenv("OPENAI_API_KEY")
16
 
17
- # Helper functions
18
  def extract_text_from_file(file):
19
- file_name = file.name
20
- if file_name.endswith(".pdf"):
21
- with pdfplumber.open(file_name) as pdf:
22
- return "\n".join([page.extract_text() or "" for page in pdf.pages])
23
- elif file_name.endswith(".docx"):
24
- doc = docx.Document(file_name)
25
- return "\n".join([para.text for para in doc.paragraphs])
26
- elif file_name.endswith(".csv"):
27
- df = pd.read_csv(file_name)
28
- return df.to_string()
29
- elif file_name.endswith(".xlsx"):
30
- df = pd.read_excel(file_name)
31
- return df.to_string()
32
- elif file_name.lower().endswith(('.png', '.jpg', '.jpeg')):
33
- return "[Image received. Please describe what you'd like me to do with it.]"
34
  else:
35
- return "[Unsupported file format.]"
 
 
36
 
37
  def transcribe_audio(audio_path):
38
- try:
39
- result = whisper_model.transcribe(audio_path)
40
- return result["text"]
41
- except Exception as e:
42
- return f"Transcription error: {str(e)}"
43
 
44
- def chat_with_openai(messages):
45
- try:
46
- response = openai.ChatCompletion.create(
47
- model="gpt-3.5-turbo",
48
- messages=messages
49
- )
50
- return response.choices[0].message.content.strip()
51
- except Exception as e:
52
- return f"❌ Error: {str(e)}"
53
 
54
- def respond(user_message, chatbot, file_upload, audio_input):
55
- messages = chatbot.copy() if chatbot else []
 
 
 
 
 
56
 
57
- # Add user message or file content
58
- if file_upload is not None:
59
- file_content = extract_text_from_file(file_upload)
60
- messages.append({"role": "user", "content": f"File uploaded. Content:\n{file_content}"})
61
- elif audio_input is not None:
62
- transcript = transcribe_audio(audio_input)
63
- messages.append({"role": "user", "content": transcript})
64
- else:
65
- messages.append({"role": "user", "content": user_message})
 
 
 
 
 
 
 
 
 
 
 
66
 
67
- reply = chat_with_openai(messages)
68
- messages.append({"role": "assistant", "content": reply})
69
- return messages, None, None # Clear inputs after response
70
 
71
- # Gradio Interface
72
- with gr.Blocks(css=".gradio-container { font-family: 'Segoe UI'; }") as demo:
73
- gr.Markdown("# πŸ€– Neobot β€” Your AI Assistant")
74
 
75
- chatbot = gr.Chatbot(type="messages", elem_id="chatbox", label="Chat")
76
- user_input = gr.Textbox(placeholder="Type a message...", label=None)
77
- file_upload = gr.File(label="πŸ“Ž Upload File", file_types=[".pdf", ".docx", ".csv", ".xlsx", ".jpg", ".jpeg", ".png"])
78
- audio_input = gr.Audio(type="filepath", label="🎀 Record Voice")
79
- submit_btn = gr.Button("Send")
80
 
81
- submit_btn.click(respond, [user_input, chatbot, file_upload, audio_input], [chatbot, file_upload, audio_input])
 
 
82
 
83
- if __name__ == "__main__":
84
- demo.launch()
 
1
  import gradio as gr
 
2
  import os
3
+ import openai
4
  import pdfplumber
5
  import docx
6
  import pandas as pd
7
  from PIL import Image
8
+ from io import BytesIO
9
+ import base64
10
  import tempfile
11
+ import whisper
12
 
13
+ openai.api_key = os.environ.get("GROQ_API_KEY")
 
14
 
15
+ # Load Whisper model
16
+ whisper_model = whisper.load_model("base")
17
 
 
18
  def extract_text_from_file(file):
19
+ if file.name.endswith(".pdf"):
20
+ with pdfplumber.open(file.name) as pdf:
21
+ text = "\n".join(page.extract_text() for page in pdf.pages if page.extract_text())
22
+ elif file.name.endswith(".docx"):
23
+ doc = docx.Document(file.name)
24
+ text = "\n".join(p.text for p in doc.paragraphs)
25
+ elif file.name.endswith(".xlsx"):
26
+ df = pd.read_excel(file.name)
27
+ text = df.to_string()
28
+ elif file.name.endswith((".png", ".jpg", ".jpeg")):
29
+ img = Image.open(file.name)
30
+ buffer = BytesIO()
31
+ img.save(buffer, format="PNG")
32
+ encoded = base64.b64encode(buffer.getvalue()).decode("utf-8")
33
+ text = f"[Image uploaded: data:image/png;base64,{encoded[:100]}... (truncated)]"
34
  else:
35
+ with open(file.name, "r", encoding="utf-8", errors="ignore") as f:
36
+ text = f.read()
37
+ return text
38
 
39
  def transcribe_audio(audio_path):
40
+ result = whisper_model.transcribe(audio_path)
41
+ return result["text"]
 
 
 
42
 
43
+ def generate_reply(history):
44
+ messages = [{"role": "system", "content": "You are a helpful assistant."}]
45
+ for user_msg, bot_msg in history:
46
+ messages.append({"role": "user", "content": user_msg})
47
+ messages.append({"role": "assistant", "content": bot_msg})
 
 
 
 
48
 
49
+ response = openai.ChatCompletion.create(
50
+ model="llama3-8b-8192",
51
+ messages=messages,
52
+ temperature=0.7
53
+ )
54
+ reply = response.choices[0].message.content
55
+ return reply
56
 
57
+ def respond(message, history):
58
+ reply = generate_reply(history + [[message, ""]])
59
+ history.append([message, reply])
60
+ return history, ""
61
+
62
+ def handle_file_upload(file, message):
63
+ if file is None:
64
+ return message
65
+ file_content = extract_text_from_file(file)
66
+ return f"{message}\n\n--- File Content Start ---\n{file_content}\n--- File Content End ---"
67
+
68
+ def handle_audio_upload(audio, message):
69
+ if audio is None:
70
+ return message
71
+ transcription = transcribe_audio(audio)
72
+ return f"{message}\n\n--- Transcription ---\n{transcription}"
73
+
74
+ with gr.Blocks(css="body { background-color: white; color: black }") as demo:
75
+ gr.Markdown("<h1 style='text-align: center;'>Neobot</h1>")
76
+ chatbot = gr.Chatbot(label="Chat", elem_id="chatbox", height=450, type="messages")
77
 
78
+ with gr.Row():
79
+ txt = gr.Textbox(placeholder="Type a message or edit transcribed/file content here...", scale=5, show_label=False)
80
+ send_btn = gr.Button("Send", scale=1)
81
 
82
+ with gr.Row():
83
+ upload_btn = gr.File(label="πŸ“Ž Upload File", file_types=[".pdf", ".docx", ".txt", ".xlsx", ".png", ".jpg", ".jpeg"])
84
+ audio_in = gr.Audio(label="πŸŽ™οΈ Upload Audio", type="filepath")
85
 
86
+ history = gr.State([])
 
 
 
 
87
 
88
+ send_btn.click(respond, [txt, history], [chatbot, txt])
89
+ upload_btn.change(handle_file_upload, [upload_btn, txt], txt)
90
+ audio_in.change(handle_audio_upload, [audio_in, txt], txt)
91
 
92
+ demo.launch()