Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,32 @@
|
|
| 1 |
|
| 2 |
import gradio as gr
|
| 3 |
-
from transformers import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from langchain_community.document_loaders import PyPDFLoader
|
| 5 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
-
from
|
| 7 |
-
from
|
| 8 |
from langchain.chains import RetrievalQA
|
| 9 |
-
from
|
| 10 |
import torch
|
| 11 |
import os
|
| 12 |
|
| 13 |
-
|
| 14 |
# --------------------------
|
| 15 |
# Load Pretrained LLaMA 2 via Hugging Face Hub
|
| 16 |
# --------------------------
|
| 17 |
llama_model_id = "meta-llama/Llama-2-7b-chat-hf" # Replace with your model ID if different
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
from huggingface_hub import login
|
| 24 |
-
|
| 25 |
-
hf_token = os.environ.get("HF_TOKEN")
|
| 26 |
-
if hf_token:
|
| 27 |
-
login(token=hf_token)
|
| 28 |
-
else:
|
| 29 |
-
raise ValueError("HF_TOKEN environment variable not found")
|
| 30 |
-
|
| 31 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 32 |
-
|
| 33 |
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# Text generation pipeline
|
| 36 |
text_pipe = pipeline(
|
|
@@ -39,7 +35,7 @@ text_pipe = pipeline(
|
|
| 39 |
tokenizer=tokenizer,
|
| 40 |
max_new_tokens=256,
|
| 41 |
temperature=0.7,
|
| 42 |
-
top_p=0.9
|
| 43 |
)
|
| 44 |
|
| 45 |
# LangChain LLM wrapper
|
|
@@ -62,8 +58,8 @@ whisper_processor = WhisperProcessor.from_pretrained("openai/whisper-small")
|
|
| 62 |
whisper_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small")
|
| 63 |
|
| 64 |
def transcribe_audio(audio):
|
| 65 |
-
|
| 66 |
-
result = whisper_model.generate(**
|
| 67 |
return whisper_processor.batch_decode(result, skip_special_tokens=True)[0]
|
| 68 |
|
| 69 |
# --------------------------
|
|
|
|
| 1 |
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import (
|
| 4 |
+
AutoTokenizer,
|
| 5 |
+
AutoModelForCausalLM,
|
| 6 |
+
pipeline,
|
| 7 |
+
WhisperProcessor,
|
| 8 |
+
WhisperForConditionalGeneration,
|
| 9 |
+
)
|
| 10 |
from langchain_community.document_loaders import PyPDFLoader
|
| 11 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 12 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 13 |
+
from langchain_community.vectorstores import Chroma
|
| 14 |
from langchain.chains import RetrievalQA
|
| 15 |
+
from langchain_community.llms import HuggingFacePipeline
|
| 16 |
import torch
|
| 17 |
import os
|
| 18 |
|
|
|
|
| 19 |
# --------------------------
|
| 20 |
# Load Pretrained LLaMA 2 via Hugging Face Hub
|
| 21 |
# --------------------------
|
| 22 |
llama_model_id = "meta-llama/Llama-2-7b-chat-hf" # Replace with your model ID if different
|
| 23 |
|
| 24 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 25 |
+
if hf_token is None:
|
| 26 |
+
raise ValueError("HF_TOKEN environment variable not found. Please add it in your secrets or environment.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
tokenizer = AutoTokenizer.from_pretrained(llama_model_id, token=hf_token)
|
| 29 |
+
model = AutoModelForCausalLM.from_pretrained(llama_model_id, token=hf_token)
|
| 30 |
|
| 31 |
# Text generation pipeline
|
| 32 |
text_pipe = pipeline(
|
|
|
|
| 35 |
tokenizer=tokenizer,
|
| 36 |
max_new_tokens=256,
|
| 37 |
temperature=0.7,
|
| 38 |
+
top_p=0.9,
|
| 39 |
)
|
| 40 |
|
| 41 |
# LangChain LLM wrapper
|
|
|
|
| 58 |
whisper_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small")
|
| 59 |
|
| 60 |
def transcribe_audio(audio):
|
| 61 |
+
audio_input = whisper_processor(audio["array"], sampling_rate=audio["sampling_rate"], return_tensors="pt")
|
| 62 |
+
result = whisper_model.generate(**audio_input)
|
| 63 |
return whisper_processor.batch_decode(result, skip_special_tokens=True)[0]
|
| 64 |
|
| 65 |
# --------------------------
|