raygiles3 commited on
Commit
419f5db
1 Parent(s): 6fe1666

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -71
app.py CHANGED
@@ -1,73 +1,49 @@
1
- import torch
2
- import os
3
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- #from langchain.llms import OpenAI
6
- from langchain.llms import HuggingFaceHub
7
-
8
- from transformers import pipeline
9
- from langchain.prompts import PromptTemplate
10
- from langchain.chains import LLMChain
11
-
12
- from ibm_watson_machine_learning.foundation_models import Model
13
- from ibm_watson_machine_learning.foundation_models.extensions.langchain import WatsonxLLM
14
- from ibm_watson_machine_learning.metanames import GenTextParamsMetaNames as GenParams
15
-
16
- my_credentials = {
17
- "url" : "https://us-south.ml.cloud.ibm.com"
18
- }
19
- params = {
20
- GenParams.MAX_NEW_TOKENS: 800, # The maximum number of tokens that the model can generate in a single run.
21
- GenParams.TEMPERATURE: 0.1, # A parameter that controls the randomness of the token generation. A lower value makes the generation more deterministic, while a higher value introduces more randomness.
22
- }
23
-
24
- LLAMA2_model = Model(
25
- model_id= 'meta-llama/llama-2-70b-chat',
26
- credentials=my_credentials,
27
- params=params,
28
- project_id="skills-network",
29
- )
30
-
31
- llm = WatsonxLLM(LLAMA2_model)
32
-
33
- #######------------- Prompt Template-------------####
34
-
35
- temp = """
36
- <s><<SYS>>
37
- List the key points with details from the context:
38
- [INST] The context : {context} [/INST]
39
- <</SYS>>
40
- """
41
-
42
- pt = PromptTemplate(
43
- input_variables=["context"],
44
- template= temp)
45
-
46
- prompt_to_LLAMA2 = LLMChain(llm=llm, prompt=pt)
47
-
48
- #######------------- Speech2text-------------####
49
-
50
- def transcript_audio(audio_file):
51
- # Initialize the speech recognition pipeline
52
- pipe = pipeline(
53
- "automatic-speech-recognition",
54
- model="openai/whisper-tiny.en",
55
- chunk_length_s=30,
56
- )
57
- # Transcribe the audio file and return the result
58
- transcript_txt = pipe(audio_file, batch_size=8)["text"]
59
- result = prompt_to_LLAMA2.run(transcript_txt)
60
-
61
- return result
62
-
63
- #######------------- Gradio-------------####
64
-
65
- audio_input = gr.Audio(sources="upload", type="filepath")
66
- output_text = gr.Textbox()
67
-
68
- iface = gr.Interface(fn= transcript_audio,
69
- inputs= audio_input, outputs= output_text,
70
- title= "Audio Transcription App",
71
- description= "Upload the audio file")
72
-
73
- iface.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import pipeline, WhisperProcessor, WhisperForConditionalGeneration, AutoModelForSeq2SeqLM, AutoTokenizer
4
+
5
+ # Initialize the Whisper processor and model
6
+ whisper_processor = WhisperProcessor.from_pretrained("openai/whisper-base")
7
+ whisper_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-base")
8
+
9
+ # Initialize the summarization model and tokenizer
10
+ summarization_model = AutoModelForSeq2SeqLM.from_pretrained("meta-llama/Llama-2-7b-hf")
11
+ summarization_tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-hf")
12
+
13
+ # Function to transcribe audio
14
+ def transcribe_audio(audio_file):
15
+ # Load audio file
16
+ audio_input, _ = whisper_processor(audio_file, return_tensors="pt", sampling_rate=16000).input_values
17
+ # Generate transcription
18
+ transcription_ids = whisper_model.generate(audio_input)
19
+ transcription = whisper_processor.decode(transcription_ids[0])
20
+ return transcription
21
+
22
+ # Function to summarize text
23
+ def summarize_text(text):
24
+ inputs = summarization_tokenizer.encode("summarize: " + text, return_tensors="pt", max_length=512, truncation=True)
25
+ summary_ids = summarization_model.generate(inputs, max_length=150, min_length=40, length_penalty=2.0, num_beams=4, early_stopping=True)
26
+ summary = summarization_tokenizer.decode(summary_ids[0], skip_special_tokens=True)
27
+ return summary
28
+
29
+ # Gradio interface
30
+ def process_audio(audio_file):
31
+ transcription = transcribe_audio(audio_file)
32
+ summary = summarize_text(transcription)
33
+ return transcription, summary
34
+
35
+ # Gradio UI
36
+ iface = gr.Interface(
37
+ fn=process_audio,
38
+ inputs=gr.Audio(source="upload", type="file"),
39
+ outputs=[
40
+ gr.Textbox(label="Transcription"),
41
+ gr.Textbox(label="Summary")
42
+ ],
43
+ title="Audio Transcription and Summarization",
44
+ description="Upload an audio file to transcribe and summarize the conversation."
45
+ )
46
+
47
+ # Launch the app
48
+ iface.launch()
49