Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,10 +6,13 @@ from datetime import datetime
|
|
6 |
from pydub import AudioSegment
|
7 |
import pdfplumber, docx, pandas as pd
|
8 |
from PIL import Image
|
|
|
9 |
|
10 |
openai.api_key = os.environ.get("GROQ_API_KEY")
|
11 |
openai.api_base = "https://api.groq.com/openai/v1"
|
12 |
|
|
|
|
|
13 |
# Chat with Groq
|
14 |
def chat_with_groq(message, state):
|
15 |
if state is None:
|
@@ -70,23 +73,23 @@ def load_chat(name):
|
|
70 |
except Exception as e:
|
71 |
return [], {"oai_history": [], "chatbot_ui": []}, f"β Could not load {name}: {e}"
|
72 |
|
73 |
-
# Transcription
|
74 |
-
|
75 |
def transcribe_audio(file):
|
76 |
if not file:
|
77 |
return ""
|
78 |
try:
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
82 |
except Exception as e:
|
83 |
return f"β Transcription error: {e}"
|
84 |
|
85 |
-
# File processor β
|
86 |
-
|
87 |
def file_to_message(file, state):
|
88 |
if file is None:
|
89 |
-
return ""
|
90 |
filename = file.name
|
91 |
ext = filename.split(".")[-1].lower()
|
92 |
try:
|
@@ -107,9 +110,6 @@ def file_to_message(file, state):
|
|
107 |
if len(text) > 8000:
|
108 |
text = text[:8000]
|
109 |
|
110 |
-
state["oai_history"].append({"role": "user", "content": f"File uploaded:\n{text}"})
|
111 |
-
state["chatbot_ui"].append({"role": "user", "content": f"File uploaded:\n{text}"})
|
112 |
-
|
113 |
return text, state
|
114 |
except Exception as e:
|
115 |
return f"β Error processing file: {e}", state
|
@@ -157,4 +157,4 @@ textarea, input[type='text'] { background: #f0f0f0; border-radius: 8px; }
|
|
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()
|
|
|
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:
|
|
|
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:
|
|
|
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
|
|
|
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()
|