Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| import tempfile | |
| import whisper | |
| import docx | |
| import pdfplumber | |
| import pandas as pd | |
| from groq import Groq | |
| from PyPDF2 import PdfReader | |
| GROQ_API_KEY = os.getenv("GROQ_API_KEY") | |
| client = Groq(api_key=GROQ_API_KEY) | |
| whisper_model = whisper.load_model("base") | |
| def read_file(file): | |
| if file is None: | |
| return "" | |
| ext = os.path.splitext(file.name)[-1].lower() | |
| try: | |
| if ext == ".txt": | |
| with open(file.name, "r", encoding="utf-8") as f: | |
| return f.read() | |
| elif ext == ".pdf": | |
| text = "" | |
| with pdfplumber.open(file.name) as pdf: | |
| for page in pdf.pages: | |
| text += page.extract_text() + "\n" | |
| return text | |
| elif ext in [".doc", ".docx"]: | |
| doc = docx.Document(file.name) | |
| return "\n".join([para.text for para in doc.paragraphs]) | |
| elif ext in [".xls", ".xlsx"]: | |
| df = pd.read_excel(file.name) | |
| return df.to_string(index=False) | |
| else: | |
| return f"[Unsupported file format: {ext}]" | |
| except Exception as e: | |
| return f"[Error reading file: {e}]" | |
| def transcribe_audio(audio): | |
| if audio is None: | |
| return "" | |
| try: | |
| audio_path = audio.name | |
| result = whisper_model.transcribe(audio_path) | |
| return result["text"] | |
| except Exception as e: | |
| return f"[Error transcribing audio: {e}]" | |
| def process_input(message, history): | |
| if not message.strip(): | |
| return history | |
| history = history or [] | |
| history.append({"role": "user", "content": message}) | |
| try: | |
| response = client.chat.completions.create( | |
| model="llama3-8b-8192", | |
| messages=history, | |
| ) | |
| reply = response.choices[0].message.content | |
| history.append({"role": "assistant", "content": reply}) | |
| except Exception as e: | |
| history.append({"role": "assistant", "content": f"[Error: {e}]"}) | |
| return history | |
| def interface_func(message, history, file, audio): | |
| file_text = read_file(file) if file else "" | |
| audio_text = transcribe_audio(audio) if audio else "" | |
| combined = "" | |
| if file_text: | |
| combined += f"{file.name} content:\n{file_text}\n\n" | |
| if audio_text: | |
| combined += f"Transcribed audio:\n{audio_text}\n\n" | |
| if message: | |
| combined += message | |
| return combined.strip() | |
| with gr.Blocks(css="textarea { font-size: 16px !important; }") as demo: | |
| gr.Markdown("<h1 style='text-align: center;'>NEOBOT</h1>") | |
| chatbot = gr.Chatbot(height=400, label="Chat") | |
| with gr.Row(): | |
| msg_box = gr.Textbox( | |
| scale=8, | |
| placeholder="Ask something...", | |
| show_label=False, | |
| container=True | |
| ) | |
| send_btn = gr.Button("Send", scale=1) | |
| with gr.Row(): | |
| file_upload = gr.File(label="Upload File", file_types=[".txt", ".pdf", ".docx", ".xlsx"]) | |
| audio_upload = gr.Audio(source="upload", type="filepath", label="Upload Audio") | |
| state = gr.State([]) | |
| send_btn.click(fn=process_input, inputs=[msg_box, state], outputs=[chatbot, state]) | |
| file_upload.change(fn=interface_func, inputs=[msg_box, state, file_upload, audio_upload], outputs=msg_box) | |
| audio_upload.change(fn=interface_func, inputs=[msg_box, state, file_upload, audio_upload], outputs=msg_box) | |
| demo.launch() | |