File size: 3,229 Bytes
aa9cd89
0fb9914
637e644
0fb9914
 
 
2ddc050
637e644
 
288fc22
637e644
0fb9914
637e644
0fb9914
637e644
 
0fb9914
bad737e
637e644
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d59737
637e644
 
 
6d59737
288fc22
637e644
 
6d59737
637e644
 
 
 
 
288fc22
637e644
 
 
 
 
 
 
288fc22
637e644
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b469735
637e644
 
 
6d59737
637e644
 
 
6d59737
637e644
b469735
637e644
 
 
3b96490
637e644
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import gradio as gr
import os
import openai
import pdfplumber
import docx
import pandas as pd
from PIL import Image
from io import BytesIO
import base64
import tempfile
import whisper

openai.api_key = os.environ.get("GROQ_API_KEY")

# Load Whisper model
whisper_model = whisper.load_model("base")

def extract_text_from_file(file):
    if file.name.endswith(".pdf"):
        with pdfplumber.open(file.name) as pdf:
            text = "\n".join(page.extract_text() for page in pdf.pages if page.extract_text())
    elif file.name.endswith(".docx"):
        doc = docx.Document(file.name)
        text = "\n".join(p.text for p in doc.paragraphs)
    elif file.name.endswith(".xlsx"):
        df = pd.read_excel(file.name)
        text = df.to_string()
    elif file.name.endswith((".png", ".jpg", ".jpeg")):
        img = Image.open(file.name)
        buffer = BytesIO()
        img.save(buffer, format="PNG")
        encoded = base64.b64encode(buffer.getvalue()).decode("utf-8")
        text = f"[Image uploaded: data:image/png;base64,{encoded[:100]}... (truncated)]"
    else:
        with open(file.name, "r", encoding="utf-8", errors="ignore") as f:
            text = f.read()
    return text

def transcribe_audio(audio_path):
    result = whisper_model.transcribe(audio_path)
    return result["text"]

def generate_reply(history):
    messages = [{"role": "system", "content": "You are a helpful assistant."}]
    for user_msg, bot_msg in history:
        messages.append({"role": "user", "content": user_msg})
        messages.append({"role": "assistant", "content": bot_msg})

    response = openai.ChatCompletion.create(
        model="llama3-8b-8192",
        messages=messages,
        temperature=0.7
    )
    reply = response.choices[0].message.content
    return reply

def respond(message, history):
    reply = generate_reply(history + [[message, ""]])
    history.append([message, reply])
    return history, ""

def handle_file_upload(file, message):
    if file is None:
        return message
    file_content = extract_text_from_file(file)
    return f"{message}\n\n--- File Content Start ---\n{file_content}\n--- File Content End ---"

def handle_audio_upload(audio, message):
    if audio is None:
        return message
    transcription = transcribe_audio(audio)
    return f"{message}\n\n--- Transcription ---\n{transcription}"

with gr.Blocks(css="body { background-color: white; color: black }") as demo:
    gr.Markdown("<h1 style='text-align: center;'>Neobot</h1>")
    chatbot = gr.Chatbot(label="Chat", elem_id="chatbox", height=450, type="messages")

    with gr.Row():
        txt = gr.Textbox(placeholder="Type a message or edit transcribed/file content here...", scale=5, show_label=False)
        send_btn = gr.Button("Send", scale=1)

    with gr.Row():
        upload_btn = gr.File(label="📎 Upload File", file_types=[".pdf", ".docx", ".txt", ".xlsx", ".png", ".jpg", ".jpeg"])
        audio_in = gr.Audio(label="🎙️ Upload Audio", type="filepath")

    history = gr.State([])

    send_btn.click(respond, [txt, history], [chatbot, txt])
    upload_btn.change(handle_file_upload, [upload_btn, txt], txt)
    audio_in.change(handle_audio_upload, [audio_in, txt], txt)

demo.launch()