Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|